常用类02-String类的常用方法

String的常用方法(一)

String s1 = "HelloWorld";
  • int length():返回字符串的长度

    System.out.println(s1.length());
    //10
    
  • char charAt(int index):返回某索引处的字符return value[index]

    System.out.println(s1.charAt(0));
    //H
    System.out.println(s1.charAt(9));
    //d
    
  • boolean isEmpty():判断是否是空字符串:return value.length == 0

    System.out.println(S1.isEmpty());
    //false
    s2 = "";
    System.out.println(s2.isEmpty());
    //true
    
  • String toLowerCase():使用默认语言环境,将String中的所有字符转换为小写

  • String toUpperCase():使用默认语言环境,将String中的所有字符转换为大写

    String s2 = s1.toLowerCase();
    System.out.println(s1);
    //HelloWorld
    System.out.println(s2);
    //helloworld
    
  • String trim():返回字符串的副本,忽略前导空白和尾部空白

    String s2 = "  he  llo world ";
    String s3 = s2.trim();
    System.out.println(s2);
    //  he  llo world 尾部
    System.out.println(s3);
    //he  llo world尾部
    
  • boolean equals(Object obj):比较字符串的内容是否相同

  • boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写

    String s2 = "helloworld";
    System.out.println(s1.equals(s2));
    //false
    System.out.println(s1.equalsIgnoreCase(s2));
    //true
    
  • String concat(String str):将指定字符串连接到此字符串的结尾,等价于用 ”+“

    String s2 = "abc";
    String s3 = s2.concat("def");
    System.out.println(s3);
    //abcdef
    
  • int compareTo(String anotherString):比较两个字符串的大小

    String s2 = "abc";
    String s3 = new String("abe");
    //涉及到字符串排序
    System.out.println(s5.compareTo(s6));
    //-2:c为99,e为101,99 - 101 = -2
    
  • String substring(int beginIndex):返回一个新字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串

  • String substring(int beginIndex):返回一个新字符串,它是此字符串的从beginIndex开始截取到endIndex(不包含)的一个子字符串

    String s2 = "123abc123";
    String s3 = s2.substring(3);
    String s4 = s2.substring(3,5);
    System.out.println(s2);
    //123abc123
    System.out.println(s3);
    //abc123
    System.out.println(s4);
    //abc
    

String的常用方法(二)

String str1 = "HelloWorld";
  • boolean endsWith(String suffix):测试此字符串是否以指定后缀结束

  • boolean startsWith(String prefix):测试此字符串是否以指定前缀开始

  • boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始

    boolean b1 = str1.endsWith("ld");
    System.out.println(b1);
    //true
    boolean b2 = str1.startsWith("He");
    System.out.println(b2);
    //true
    boolean b3 = str1.startsWith("ll",2);
    //true
    
  • boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true

    String str2 = "Wo";
    System.out.println(str1.contains(str2));
    //true
    
  • int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引

  • int indexOf(String str,int fromIndex):返回指定字符串在此字符串中第一次出现处的索引,并

    System.out.println(str1.indexOf("lo"));
    //3
    System.out.println(str1.indexOf("lol"));
    //-1
    System.out.println(str1.indexOf("lo",5));
    //-1
    
  • int lastIndexOf(String str):返回指定在此字符串中最右边出现处的索引

  • int lastIndexOf(String str,int fromIndex):返回指定字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索

    String str2 = "helloworld"
    System.out.println(st2.lastIndexOf("or"));
    //7
    System.out.println(st2.lastIndexOf("or",6));
    //-1
    
  • 以上四种方法 indexOf 和 lastIndexOf 方法如果未找到都是返回 -1

  • 特别的,在存在唯一的一个str 或 不存在str 时,indexOf(str) 和 lastIndexOf(str) 的返回值相同

String的常用方法(三)

String str1 = "一二三三二一";

替换

  • String replace(char oldChar,char newChar):返回一个新的字符串,它是通过 newChar 替换此字符串中出现的所有的oldChar得到的

    String str2 = str1.replace('一', '三');
    System.out.println(str1);
    //一二三三二一
    System.out.println(str2);
    //三二三三二三
    
  • String replace(CharSequence target,CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串

  • String replaceAll(String regex,String replacement):使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串

    String str = "123321abccba";
    String string = str.replaceAll("\\d+",",");
    System.out.println(string);
    //,abccba
    
  • String replaceFirst(String regex,String replacement):使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串

匹配

  • boolean matches(String regex):告知此字符串是否匹配给定的正则表达式

    String str = "123321";
    boolean matches = str.matches("\\d+");
    System.out.println(matches);
    //true
    String tel = "1111-1111111";
    boolean result = tel.matches("1111-\\d{7,8}");
    System.out.println(result);
    //true
    

切片

  • String[] split(String regex):根据给定正则表达式的匹配拆分此字符串

  • String[] split(String regex,int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中

    String str = "hello|world|java";
    String[] strs = str.split("\\|");
    for (int i = 0; i < strs.length; i++) {
        System.out.println(strs[i]);
    }
    //hello
    //world
    //java
    //
    System.out.println();
    String str0 = "hello.world.java";
    String[] str1 = str0.split("\\.");
    for (int i = 0; i < strs.length; i++) {
        System.out.println(str1[i]);
    }
    //hello
    //world
    //java
    //
    

posted on 2022-02-26 18:09  Baby091  阅读(29)  评论(0编辑  收藏  举报

导航