字符串的学习和练习

知识点


/*
 * indexOf()
 * lastIndexOj()
 * replace()
 * split()
 * charAt()
 * *subString()频率很高的方法.用来截取字符串
 */
public class StringClass {
    public static void main(String[] args) {
        String s = "   abcdefg  ";
        String s1 = new String("a   b    c");
        //字符串长度
        System.out.println(s.length());
        //trim方法.去掉字符串两边的空格
        System.out.println(s.trim().length());
        System.out.println(s1.length());
        //trim不能去除中间的空格
        System.out.println(s1.trim().length());
        //截取字符串部分
        String s2 = "再来一瓶";
        System.out.println(s2.substring(2));
        System.out.println(s2.substring(1,3));
        //index找出指定字符的位置(索引).不只是找字符.也可以找串
        //找到子字符在当前字符中的首字母的索引
        String s3 = "2121abcdef21g";
        System.out.println(s3.indexOf("cd"));
        //Ascll码也是可以的
        System.out.println(s3.indexOf(97));
        //第二个参数是指查找起点
        System.out.println(s3.indexOf("21",7));
        //lastIndexOf查找最后一个关键字所在位置
        String s4 = "123321123";
        System.out.println(s4.lastIndexOf("1"));
        //replace 替换指定字符
        String s5 = "a b c d     ";
        String s6 = s5.replace(' ', '@');
        System.out.println(s6);
        //split分割
        String s7 = "a|b,c d ef     ";
        String arr[] = s7.split(" ");
        for (String string : arr) {
            System.out.println(string);
        }
        //charAt获取某个位置的字符
        String s8 = "abcdefg";
        System.out.println(s8.charAt(0));
    }
}

实例:检查是不是.java文件

public void java1(String s) {
        String  string = "";
        for (int i = s.lastIndexOf("."); i < s.length(); i++) {
            string += s.charAt(i);
        }
        if (string.equals(".java")) {
            System.out.println("您输入的是Java文件");
        }else{
            System.out.println("不是Java文件");
        }
    }
    public static void main(String[] args) {
        Scanner scanner  = new Scanner(System.in);
        Test2 test  =  new Test2();
        System.out.println("请输入您的文件名:");
        String string = scanner.next();
        test.java1(string);
        }

实例:将字符串中的i改为1.O该问0

public String phone(String s) {
        String s1 = s.replace("O", "0");
        String s2 = s1.replace("i", "1");
        return s2;
    }
    public static void main(String[] args) {
        Scanner scanner  = new Scanner(System.in);
        Test2 test  =  new Test2();
        System.out.println("请输入手机充值卡密码:");
        String string2 = scanner.next();
        String string3 = test.phone(string2);
        System.out.println(string3);
    }
}

实例:字符串My_English_NaMe改为myENGLISHNAME

public String daxiaoxie(String str) {
        String[] arr = str.split("_");
        String s1 = arr[0];
        String s2 = arr[1];
        String s3 = arr[2];
        String string ="";
        string += s1.toLowerCase();
        string += s2.toUpperCase();
        string += s3.toUpperCase();
        return string;
    }
    public static void main(String[] args) {
        System.out.println("字符串My_English_NaMe改为myENGLISHNAME:");
        String string4 = "My_English_NaMe";
        System.out.println(test.daxiaoxie(string4));

    }

实例:找出字符串中数字.英文.空格和其他字符的数量

public void T1(String s) {
        int num1 = 0;//这是英文的个数
        int num2 = 0;//这是数字的个数
        int num3 = 0;//这是空格的个数
        int num4 = 0;//这是其他字符的个数
        for (int i = 0; i < s.length(); i++) {
            char key = s.charAt(i);
            if (key >= 'A'&&key <= 'Z') {
                num1 += 1;
            }else if(key >= 'a'&&key <= 'z') {
                num1 += 1;
            }else if(key >= 48 && key <= 57) {
                num2 += 1;
            }else if(key == ' ') {
                num3 += 1;
            }else{
                num4 += 1;
            }

        }
        System.out.println("英文字母的数量为"+num1);
        System.out.println("数字的数量为"+num2);
        System.out.println("空格的数量为"+num3);
        System.out.println("其他字符的数量为"+num4);
    }
    public static void main(String[] args) {
        Test3 test3 = new Test3();
        String string = "    abCD1234#$%&";
        test3.T1(string);
    }

这里写图片描述

实例:去掉字符串中间的空格但中间的空格不去掉

//这行代码可以完成
System.out.println(s2.replace(s2.trim(),s2.trim().replace(" ", "")));

实例:回文串的判定

public void T5(String string) {
        int length = string.length();
        int number = 0;
        if (length % 2 ==0) {
            for (int i = 0; i < length; i++) {
                if(string.charAt(i) == string.charAt((length)-i-1)){
                    number +=1;
                }else{
                    break;
                }
            }
            if (number == length) {
                System.out.println("这是个回串文");
            }else{
                System.out.println("这不是个回串文");
            }
        }else{
            for (int i = 0; i < (length-1)/2; i++) {
                if(string.charAt(i) == string.charAt((length)-i-1)){
                    number +=1;
                }else{
                    break;
                }

        }
            if (number == (length-1)/2) {
                System.out.println("这是个回串文");
            }else{
                System.out.println("这不是个回串文");
            }

    }
}
    public static void main(String[] args) {
        Test5 test5 = new Test5();
        test5.T5("abba");
    }

实例:删除字符串中的某个字符

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("字符串");
        String string = scanner.next();
        System.out.println("字符");
        String string2 = scanner.next();
        for (String string3 : string.split(string2)) {
            System.out.print(string3);
        }
    }

实例:每个字符后面插入一个空格

public static void main(String[] args) {
        String string = "abcd";
        String string2 = "";
        for (int i = 0; i < string.length(); i++) {
            string2 += string.charAt(i)+" ";
        }
        System.out.println(string2);
    }

实例:随机生成一个4位的验证码(包含英文大小写和数字)

public static void main(String[] args) {
        Random random = new Random();
        int m=0;
        do{
            int n = random.nextInt(123);
            if (n <= 122&& n >= 97) {
                System.out.print((char)n);
                m++;
                continue;
            }else if(n <= 90&& n >= 65){
                System.out.print((char)n);
                m++;
                continue;
            }else if(n <=57&& n >= 48 ){
                System.out.print((char)n);
                m++;
                continue;
            }else{
                continue;
            }
        }while(m<4);
    }

实例:学生抽奖

public static void main(String[] args) {
        ArrayList<String> students = new ArrayList<String>();
        Random random = new Random();
        students.add("古一");
        students.add("林二");
        students.add("张三");
        students.add("李四");
        students.add("王五");
        students.add("赵六");
        students.add("刘七");
        students.add("胡八");
        students.add("陈九");
        students.add("王十");
        students.add("古十");
        students.add("林九");
        students.add("张八");
        students.add("李七");
        students.add("王六");
        students.add("赵五");
        students.add("刘四");
        students.add("胡三");
        students.add("陈二");
        students.add("王一");

//      int length = students.size();
        System.out.println("\n三等奖的同学是:");
        for (int i = 0; i < 6; i++) {
            int n = random.nextInt(20-i);
            System.out.print(students.get(n)+"\t");
            students.remove(students.get(n));

        }
        System.out.println("\n二等奖的同学是:");
        for (int i = 0; i < 2; i++) {
            int n = random.nextInt(14-i);
            System.out.print(students.get(n)+"\t");
            students.remove(students.get(n));
        }
        System.out.println("\n一等奖的同学是:");
        for (int i = 0; i < 1; i++) {
            int n = random.nextInt(12-i);
            System.out.println(students.get(n));
            students.remove(students.get(n));
        }
    }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值