String s ="青春无悔";int a = s.lenght();System.out.println("字符长度为:"+ a);//运行结果:字符长度为:4
2.比较两个字符串是否相等(区分大小写)-equals()方法
String s ="青春无悔";String s1 ="青春无悔";boolean ok = s.equals(s1);System.out.println("判断结果为:"+ ok);//运行结果:判断结果为:trueString s ="abc";String s1 ="aBc";boolean ok = s.equals(s1);System.out.println("判断结果为:"+ ok);//运行结果:判断结果为:false
3.比较两个字符串是否相等(忽略大小写)-equalsIgnoreCase()方法
String s ="青春无悔";String s1 ="青春无悔";boolean ok = s.equalsIgnoreCase(s1);System.out.println("判断结果为:"+ ok);//运行结果:判断结果为:trueString s ="abc";String s1 ="aBc";boolean ok = s.equalsIgnoreCase(s1);System.out.println("判断结果为:"+ ok);//运行结果:判断结果为:true
4.将字符串转换成小写-toLowerCase()方法
String s ="AbCDe";String s1 = s.toLowerCase();System.out.println("转换结果为:"+ s1);//运行结果:转换结果为:abcde
5.将字符串转换成大写-toUpperCase()方法
String s ="AbCDe";String s1 = s.toUpperCase();System.out.println("转换结果为:"+ s1);//运行结果:转换结果为:ABCDE
6.链接两个字符串-concat()方法或‘+’号
String s ="青春无悔";String s1 ="青春无悔";String ok = s.concat(s1);System.out.println("链接结果为:"+ ok);//运行结果:链接结果为:青春无悔青春无悔String s ="abc";String s1 ="aBc";String ok =(s+s1);System.out.println("链接结果为:"+ ok);//运行结果:链接结果为:abcaBc
7.查找一个字符或(字符串)在某个字符串中第一次出现的位置-indexOf()
String s ="青春无悔";int a = s.indexOf('春');System.out.println("查找结果为:"+ a);//运行结果:查找结果为:1
8.查找一个字符或(字符串)在某个字符串中最后一次出现的位置-lastIndexOf()
String s ="青春无悔";int a = s.lastIndexOf("无悔");System.out.println("查找结果为:"+ a);//运行结果:查找结果为:2
9.截取到字符串从指定的位置开始到最后一个位置(包含该开始位置的字符)-substring()
String s ="青春无悔";String a = s.substring(1);System.out.println("截取结果为:"+ a);//运行结果:截取结果为:春无悔
10.提取两个下标之间的字符串-substring()
String s ="青春无悔";String a = s.substring(1,3);System.out.println("截取结果为:"+ a);//运行结果:截取结果为:春无
11.忽略字符串前后空格-trim()
String s =" 青 春 无 悔 ";String a = s.trim();System.out.println("截取结果为:"+ a);//运行结果:截取结果为:青 春 无 悔//想忽略全部空格请使用替换方法
12.从字符串中取出指定位置的字符-charAt()
String s ="青春无悔";char c = s.charAt(2);System.out.println("提取结果为:"+ c);//运行结果:提取结果为:无
13.返回值为char数组类型,将字符串变成一个字符数组-toCharArray()
String s ="青春无悔";char c[]= s.toCharArray();System.out.print("结果为:");for(int i =0; i < c.length; i++){System.out.print(c[i]+",");}//运行结果:结果为:青,春,无,悔,
14.根据给定的正则表达式的匹配来拆分此字符串,形成一个新的String数组-split()
String s ="青春无悔";String[] a = s.split("春");System.out.print("结果为:");for(int i =0; i < a.length; i++){System.out.print(a[i]+",");}//运行结果:结果为:青,无悔
15.判断一个字符串里面是否包含指定的内容-contains()
String s ="青春无悔";boolean ok = s.contains("春");System.out.print("结果为:"+ok);//运行结果:结果为:true
16.测试此字符串是否以指定的前缀开始-startsWith()
String s ="青春无悔";boolean ok = s.startsWith("青");System.out.print("结果为:"+ok);//运行结果:结果为:true
17.测试此字符串是否以指定的后缀结束-endsWith()
String s ="青春无悔";boolean ok =s.endsWith("无悔");System.out.print("结果为:"+ok);//运行结果:结果为:true
18.将第一次出现的某个内容替换成指定的内容-repalceFirst()
String s ="青春无悔";String s1 = s.repalceFirst("青","春");System.out.print("结果为:"+s1);//运行结果:结果为:春春无悔
19.将字符串1中存在的字符串2删除b个或(按照字符串2进行拆分b次)-split()
String s ="青 春 无 悔 ";String[] a = s.split(" ",2);System.out.print("结果为:");for(int i =0; i < a.length; i++){//数组长度为2System.out.print(a[i]+",");}//运行结果:结果为:青,春 无 悔
20.转换为String类型-toString()或‘+“”’
int s =1;String a = s.toString();String b = s+"";System.out.print("结果为:"+(a+b));//运行结果:结果为:11
21.将字符串2接到字符串1后面-append()
String s ="青春";String s1 ="无悔";String s2 = s.append(s1);System.out.print("结果为:"+s2);//运行结果:结果为:春春无悔
22.在指定的位置插入指定的值(参数可以是任何数据类型)-insert()
String s ="青春无悔";String s1 = s.insert(2,"哈哈");System.out.print("结果为:"+s1);//运行结果:结果为:春春哈哈无悔String s2 = s.insert(2,"哈哈哈哈",2);System.out.print("结果为:"+s2);//运行结果:结果为:春春哈哈无悔//insert()功能是很强大的