• 【String类的常用方法】



    字符串构造

    public class Test {
        public static void main(String[] args) {
    
            String str = "hello";
            System.out.println(str);
    
            String str2 = new String("hehehe");
            System.out.println(str2);
    
            char[] array = {'a','b','c'};
            String str3 = new String(array);
            System.out.println(str3);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    public class Test {
        public static void main(String[] args) {
            String s1 = new String("hello");
            String s2 = new String("world");
            String s3 = s1;
            System.out.println(s3);
    
            String s4 = "";//存储0
            System.out.println(s4.length());
            System.out.println(s4.isEmpty());
    
            String s5 = null;//不存任何数
            System.out.println(s5.length());
            System.out.println(s5.isEmpty());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. String是引用类型,内部并不存储字符串本身,
      在这里插入图片描述

    String对象的比较

    1.== //比较地址
    2. equals //比较值相不相等
    3.compareTo //比较值的大小
    4.compareToIgnoreCase//忽略大小写比较

    public class Test {
        public static void main(String[] args) {
            String s1 ="Student";
            String s2 ="Student";
            System.out.println(s1==s2);//比较值
    
            String s3 = new String("JAVA");
            String s4 = new String("JAVA");
            System.out.println(s3 == s4);//比较地址一样不一样
            System.out.println(s3.equals(s4));//比较值
    
            String s5 ="abc";
            String s6 ="adc";
            //s5>s6 返回正数
            //s5==s6 返回0
            //s5
            System.out.println(s5.compareTo(s6));
    
    
            //忽略大小写比较
            String s7 ="abc";
            String s8 ="ABC";
            System.out.println(s7.compareToIgnoreCase(s8)); //0
    
        }
        public static void main3(String[] args) {
            String s1 = new String("hello");
            String s2 = new String("world");
            String s3 = s1;
            System.out.println(s3);//hello
    
            String s4 = "";//存储0
            System.out.println(s4.length());
            System.out.println(s4.isEmpty());
    
            String s5 = null;//不存任何数
           // System.out.println(s5.length());
            System.out.println(s5.isEmpty());
        }
    
        public static void main2(String[] args) {
    
            String str = "hello";
            System.out.println(str);
    
            String str2 = new String("hehehe");
            System.out.println(str2);
    
            char[] array = {'a','b','c'};
            String str3 = new String(array);
            System.out.println(str3);//abc
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    字符串查找

    charAt

    找到字符串某个字符的下标的元素

    public class Test {
    
        public static void main(String[] args) {
    
            //charAt
            String s1 ="English";
            char ch = s1.charAt(3);//3下标的值
            System.out.println(ch);
    
            //遍历字符串
            for (int i = 0; i <s1.length() ; i++) {
                char sh = s1.charAt(i);
                System.out.print(sh);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    indexof

    int indexOf(int ch)
    在字符串中 第一次出现某个字符的下标,没有返回-1

    public class Test {
        public static void main(String[] args) {
            String str = "abacabc";
           int index = str.indexOf('a');//字符串中第一次出现字符a的下标
            System.out.println(index);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    int indexOf(int ch, int fromIndex)
    从fromIndex位置开始找字符第一次出现的位置 没有返回-1

    public class Test {
        public static void main(String[] args) {
            String str = "abacabc";
            int index = str.indexOf('a',3);
            System.out.println(index);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    int indexOf(String str)
    返回字符串第一次出现的位置,没有返回-1

    public class Test {
        public static void main(String[] args) {
            String str = "abacabc";
            int index = str.indexOf("abc");
            System.out.println(index);//4
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    int indexOf(String str, int fromIndex)
    从fromIndex位置开始找字符串第一次出现的位置,没有返回-1

    public class Test {
        public static void main(String[] args) {
            String str = "acabacb";
            int index = str.indexOf("ab",2);
            System.out.println(index);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    int lastIndexOf(int ch)
    从后往前找,返回字符第一次出现的位置,没有返回-1

    public class Test {
        public static void main(String[] args) {
            String str = "acabacb";
            int index = str.lastIndexOf('b');
            System.out.println(index);//6
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    int lastIndexOf(int ch, int fromIndex)
    从fromIndex位置开始找,从后往前找字符第一次出现的位置,没有返
    回-1

    public class Test {
        public static void main(String[] args) {
            String str = "acabacb";
            int index = str.lastIndexOf('b',5);
            System.out.println(index);//3
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    int lastIndexOf(String str)
    从后往前找,返回字符串第一次出现的位置,没有返回-1

    public class Test {
        public static void main(String[] args) {
            String str = "acabacb";
            int index = str.lastIndexOf("ac");
            System.out.println(index);//4
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    int lastIndexOf(String str, int fromIndex)
    从fromIndex位置开始找,从后往前找字符串第一次出现的位置,没有返
    回-1

    public class Test {
        public static void main(String[] args) {
            String str = "acabacb";
            int index = str.lastIndexOf("ac",3);
            System.out.println(index);//0
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    转化

    1. 数值和字符串转化

    数字转字符串

    public class Test {
        public static void main(String[] args) {
            //数字转字符串
            String s1 =  String.valueOf(1919);//整型
            String s2 =  String.valueOf(19.9);//浮点型
            System.out.println(s1);//变成字符串了
            System.out.println(s2);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    字符串转数字

    public class Test {
        public static void main(String[] args) {
           int date = Integer.parseInt("198");//整型转字符串
          double date2 = Double.parseDouble("15.55");//浮点型转字符串
            System.out.println(date);
            System.out.println(date2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.大小写转换 toUpperCase toLowerCase

    public class Test {
        public static void main(String[] args) {
            String s1 = "hello";
            System.out.println(s1.toUpperCase());//HELLO
            
            String s2 = "WORLD";
            System.out.println(s2.toLowerCase());//world
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.字符串转数组 toCharArray

    public class Test {
        public static void main(String[] args) {
            String s1 = "hello";
            char[] array = s1.toCharArray();
            System.out.println(Arrays.toString(array));//Arrays.toString( )是打印数组的
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.数组转字符串

    public class Test {
        public static void main(String[] args) {
            char[] array ={'h','e','l','l','o'};
            String s1 = new String(array);
            System.out.println(s1);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.格式化 format

    public class Test {
        public static void main(String[] args) {
            String s =String.format("%d-%d-%d",2023,11,06);
            System.out.println(s);//2023-11-06
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    字符串替换

    String replaceAll(String regex, String replacement)

    替换所有的指定内容

    public class Test {
        public static void main(String[] args) {
            String str = "abbaacca";
            System.out.println(str.replaceAll("b","q"));//aqqaacca
            System.out.println(str);//abbaacca
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    String replaceFirst(String regex, String replacement)

    替换首个内容

    public class Test {
        public static void main(String[] args) {
            String str = "abbaacca";
            String s1 = str.replaceFirst("b","88");
            System.out.println(s1);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串

    字符串拆分

    以空格拆分

    public class Test {
        public static void main(String[] args) {
            String str = "hello beautiful world";
            String [] s1 = str.split(" ");
            
            for (int i = 0; i < s1.length; i++) {
                System.out.println(s1[i]);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    以空格分开,分成两组

    public class Test {
        public static void main(String[] args) {
            String str = "hello beautiful world";
            String [] s1 = str.split(" ",2);
    
            for (int i = 0; i < s1.length; i++) {
                System.out.println(s1[i]);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    特殊字符拆分

    特殊字符作为分割符可能无法正确切分, 需要加上转义 \ \

    public class Test {
    // 拆分IP地址
        public static void main(String[] args) {
            String str = "198.153.1.2";
            String [] s1 = str.split("\\.");
    
            for (int i = 0; i < s1.length; i++) {
                System.out.println(s1[i]);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    多个分隔符拆分

    如果一个字符串中有多个分隔符,可以用"|"作为连字符

    public class Test {
        public static void main(String[] args) {
            String str2 = "wo=he=zhangsan&zai&xuexi ";
            String [] array =str2.split("=|&");
            for (int i = 0; i < array.length; i++) {
                System.out.println(array[i]);
            }
        }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    字符串的截取

    从一个完整的字符串之中截取出部分内容。

    String substring(int beginIndex, int endIndex)

    截取部分内容

    public class Test {
        public static void main(String[] args) {
            String str3 = "abcaac";
            String s4 = str3.substring(0,3);
            System.out.println(s4);//abc
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    String substring(int beginIndex)

    从指定索引截取到结尾

    public class Test {
        public static void main(String[] args) {
            String str3 = "abcaac";
            String s4 = str3.substring(3);
            System.out.println(s4);//aac
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其他操作

    String trim()

    去掉字符串中的左右空格,保留中间空格

    public class Test {
        public static void main(String[] args) {
            String str4 = " abc aac ";
            String s5 = str4.trim();
            System.out.println(s5);//abc aac 
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    String类 练习

    给定一个字符串 s ,找到它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1 。

    public class Test {
        public int firstUniqChar(String s) {
            int[] count = new int[26]; //创建一个数组
            //每遍历一个字符,就把字符对应的下标++一次
            for(int i = 0;i < s.length();i++){
                char ch = s.charAt(i);
                count[ch-'a']++;
    //如果是a-a就是97-97=0,a就能在0下标位置,b-a就是97-97,b就在1下标位置,
    //这个方法适用于找字符。
            }
            for(int i = 0;i < s.length();i++) {
            //再遍历一次,如果有i的下标==1,则返回i
                char ch = s.charAt(i);
                if(count[ch-'a'] == 1){
                    return i;
                }
            }
            return -1;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    计算字符串最后一个单词的长度,单词以空格隔开
    运用String类中多个库方法计算

    public class Test {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String str = in.nextLine();//输入
           while(in.hasNextLine()){
               int index = str.lastIndexOf(' ');//从后往前找第一个空格
               String s4 = str.substring(index+1);//截取空格后到末尾的字符串
               System.out.println(s4.length());//求字符的长度
           }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    Centos7 linux 中 防火墙操作命令及SSH远程连接
    自学软件测试3个月,成功入职《字节》后的面试心得总结
    1.6 泛化能力
    react组件多次渲染问题
    CentOS-7下安装及配置vsftpd详细步骤(可匿名访问)
    字符串6——实现 strStr()
    RocketMQ关于指标数据的总结
    Linux实战案例——使用LNMP+WordPress搭建个人博客网站
    Python编程从入门到实践 第九章:类 练习答案记录
    完美卸载mac电脑里的软件及残留清理教程
  • 原文地址:https://blog.csdn.net/2301_76496134/article/details/134231833