一文看懂String类中的常用方法

1、int length(): 返回字符串的长度: return value.length
2、char charAt(int index): 返回某索引处的字符return value[index]
3、boolean isEmpty(): 判断是否是空字符串: return value.length == 0

String str = " HELLO world ";

System.out.println(str.length());//13
System.out.println(str.charAt(0));//" "第一个空格
System.out.println(str.charAt(9));//r
System.out.println(str.isEmpty());//false

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

String str = " HELLO world ";

String s1 = str.toLowerCase();//转换所有字符为---->小写
String s2 = str.toUpperCase();//转换所有字符为---->大写

System.out.println(s1);//" hello world "
System.out.println(s2);//" HELLO WORLD "
System.out.println(str);//" HELLO world "   不改变原值

6、String trim(): 返回字符串的副本, 忽略前导空白和尾部空白

String str = " HELLO world ";

String s3 = str.trim();//去除字符串首尾空格
System.out.println(s3);//"HELLO world"

7、boolean equals(Object obj): 比较字符串的内容是否相同
8、boolean equalsIgnoreCase(String anotherString): 与equals方法类似, 忽略大小写

String s1 = "HELLOWORLD";
String s2 = "helloworld";

System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s2));//true    忽略大写小写比较

9、String concat(String str): 将指定字符串连接到此字符串的结尾。 等价于用“+”

String s3 = s1.concat("降龙十八掌");//连接字符串,等价于 “+”
System.out.println(s3);//HELLOWORLD降龙十八掌

10、int compareTo(String anotherString): 比较两个字符串的大小

String s4 = "abc";//97、98、99
String s5 = new String("abg");//97、98、103
System.out.println(s4.compareTo(s5));//-4   遇到相等跳过,遇到不同作差,输出
String s6 = "aag";//97、97、103
System.out.println(s4.compareTo(s6));//1

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

String s7 = "降龙十八掌、六脉神剑、乾坤大挪移";
String s8 = s7.substring(6);//切片操作
String s9 = s7.substring(6,10);左闭右开[)

System.out.println(s8);//六脉神剑、乾坤大挪移
System.out.println(s9);//六脉神剑

13、boolean endsWith(String suffix): 测试此字符串是否以指定的后缀结束

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

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

String s1 = "六脉神剑、九阳神功、一阳指";
boolean s2 = s1.startsWith("六");		//以xx开始
System.out.println(s2);//true

boolean s3 = s1.startsWith("九阳",5);		//从第index处 以xx开始
System.out.println(s3);//true

boolean s4 = s1.endsWith("指");			//以xx结束
System.out.println(s4);//true

16、boolean contains(CharSequence s): 当且仅当此字符串包含指定的 char 值序列时,返回 true

String s1 = "六脉神剑、九阳神功、一阳指";

String s5 = "九阳神功";
System.out.println(s1.contains(s5));//true

17、int indexOf(String str): 返回指定子字符串在此字符串中第一次出现处的索引

18、int indexOf(String str, int fromIndex): 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
19、int lastIndexOf(String str): 返回指定子字符串在此字符串中最右边出现处的索引

20、int lastIndexOf(String str, int fromIndex): 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
注: indexOf和lastIndexOf方法如果未找到都是返回-1

String s1 = "六脉神剑、九阳神功、一阳指";

System.out.println(s1.indexOf("神剑"));//2
System.out.println(s1.indexOf("神剑", 6));//-1
System.out.println(s1.lastIndexOf("神"));//7
System.out.println(s1.lastIndexOf("神", 5));//2

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

22、String replace(CharSequence target, CharSequence replacement): 使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

String s1 = "六脉神剑、九阳神功、一阳指";
System.out.println(s1.replace("神", "鬼"));//六脉鬼剑、九阳鬼功、一阳指

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

String str = "12hello34world5java7891mysql456";
//把字符串中的数字替换成 ","如果结果中开头和结尾有,的话去掉
String string = str.replaceAll("\\d+", ",").replaceAll("^,|,$", "");//正则表达式
System.out.println(string);
//hello,world,java,mysql

24、String replaceFirst(String regex, String replacement) : 使用给定的replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

String s1 = "六脉神剑、九阳神功、一阳指";
System.out.println(s1.replace("神", "鬼"));//六脉鬼剑、九阳鬼功、一阳指
String str = "1111AAAA2222BBBB999";
//把字符串中的数字替换成,,如果结果中开头和结尾有,的话去掉
String string = str.replaceFirst("\\d+", ",");
System.out.println(string);//,AAAA2222BBBB999

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

String str = "12345";
//判断str字符串中是否全部有数字组成,即有1-n个数字组成
boolean matches = str.matches("\\d+");
System.out.println(matches);//true


String tel = "0476-4534289";
//判断这是否是一个赤峰的固定电话
boolean result = tel.matches("0476-\\d{7,8}");
System.out.println(result);//true

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

27、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.print(strs[i] + "\t");
}
//hello	world	java
System.out.println();
String str2 = "hello.world.java";
String[] strs2 = str2.split("\\.",2);
for (int i = 0; i < strs2.length; i++) {
    System.out.print(strs2[i] + "\t");
}
//hello	world.java	

posted on 2021-09-19 12:01  Tianhao丶  阅读(295)  评论(0编辑  收藏  举报

导航