


package com.atguigu.java2;import org.junit.Test;/** * * * @author Dxkstart * @create 2021-05-09 10:52 */public class StringMethodTest1 { /* 1.int length():返回字符串的长度:return value.length 2.char charAt(int index):返回某索引处的字符return value[index] 3.boolean isEmpty():判断是否是空字符串:return value.length == 0 4.boolean toLowerCase():使用默认语言环境,将String 中的所有字符转换为小写 5.boolean toUpperCase():使用默认语言环境,将String 中的所有字符转换为大写 6.String trim():返回字符串的副本,忽略前导空白和尾部空白 7.boolean equals(Object obj):比较字符串的内容是否相同 8.boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写 9.String concat(String str):将指定字符串连接到此字符串的结尾。等价于“ + ” 10.(重要)int compareTo(String anotherString):比较两个字符串的大小 11.String substring(int beginIndex):返回一个新的字符串, 它是此字符串的从beginIndex开始截取的子串。 12.String substring(int beginIndex,int endIndex):返回一个新的字符串, 它是此字符串的从beginIndex(包含)开始到endIndex(不包含)之间的子串。 像是数学中的[a,b) 13.boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束 14.boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始 15.boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串 是否以指定前缀开始 16.boolean contains(CharSequence s):当且仅当次字符串包含指定的char值序列时,返回true 17.int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引 18.int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引, 从指定的索引开始 19.int lastIndexOf(String str):返回指定字符串在此字符串中最右边出现处的索引 20.int laseIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引, 从指定的索引开始返向搜索 注:indexOf和lastIndexOf方法如果未找到都是返回-1 //替换: 21.String replace(char oldChar,char newChar):返回一个新的子字符串,它是通过用newChar替换此 字符串中出现的所有oldChar得到的 22.String replace(CharSequence target,CharSequence replacement):使用指定的字面值替换序列 替换 此字符串所有匹配字面值目标序列的子字符串 23.String replaceAll(String regex,String replacement):使用给定的replacement替换此字符串所有 匹配给定的正则表达式的子字符串 24. //匹配: 25. //切片: */ @Test public void test1(){ String s1 = "HelloWorld"; //1. System.out.println(s1.length()); //2. System.out.println(s1.charAt(0)); System.out.println(s1.charAt(9));// System.out.println(s1.charAt(10));越界 //3. System.out.println(s1.isEmpty()); //4. String s2 = s1.toLowerCase(); System.out.println(s1);//s1不可变的,仍然为原来的字符串 System.out.println(s2);//改成小写以后的字符串 //5. System.out.println(s1.toUpperCase());//改成大写 //6. String s3 = " he llo Worle "; String s4 = s3.trim(); System.out.println("-----" + s3 + "-----"); System.out.println("-----" + s4 + "-----");//去除了首尾空白 } @Test public void test2(){ String s1 = "HelloWorld"; String s2 = "helloworld"; //7. System.out.println(s1.equals(s2)); //8. System.out.println(s1.equalsIgnoreCase(s2)); //9. System.out.println(s1.concat(" test")); //10. String s3 = "abc"; String s4 = "ABC"; System.out.println(s3.compareTo(s4));//涉及到字符串排序 //11. String s5 = "北京尚硅谷教育"; System.out.println(s5.substring(2));//从[2]开始截取 //12. System.out.println(s5.substring(2,5)); } @Test public void test3(){ //13. String str1 = "helloworld"; boolean b1 = str1.endsWith("ld"); System.out.println(b1); //14. System.out.println(str1.startsWith("h")); //15. System.out.println(str1.startsWith("ll",2)); //从[2]开始的字符串是否是子串 //16. String str2 = "wo"; System.out.println(str1.contains(str2)); //17. System.out.println(str1.indexOf("o")); //18. System.out.println(str1.indexOf("lo",3)); //19. String str3 = "hellorworld"; System.out.println(str3.lastIndexOf("or")); //20. System.out.println(str3.lastIndexOf("or",5)); } @Test public void test4(){ String str1 = "helloworld"; //21. System.out.println(str1.replace('l','S')); System.out.println(str1.replace("lo","p")); }}