String的常用方法

String的常用方法有以下几种:

一:charAt【返回指定索引处的 char 值】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println("charAt:"+name.charAt(3));

打印的结果:

charAt:李

二:compareTo【按字典顺序比较两个字符串】

案例:

String a1="王";
String b1="家";

System.out.println("compareTo:"+b1.compareTo(a1));

打印的结果:

compareTo:-6101

三:compareToIgnoreCase【按字典顺序比较两个字符串,不考虑大小写】

案例:

String a2="A";
String b2="c";

System.out.println("compareToIgnoreCase:"+b2.compareToIgnoreCase(a2));

打印的结果:

compareToIgnoreCase:2

四:concat【将指定字符串连接到此字符串的结尾】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println("concat:"+name.concat(",刘琦"));

打印的结果:

concat:张三,李四,王五,赵六,赵七,刘琦

五:contains【当且仅当此字符串包含指定的 char 值序列时,返回 true】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println("contains:"+name.contains("赵"));

打印的结果:

contains:true

六:contentEquals【将此字符串与指定的 CharSequence或者StringBuffer比较,相同时返回true】

案例:

String name="张三,李四,王五,赵六,赵七";

CharSequence charSequence="张三,李四,王五,赵六,赵七";

StringBuffer stringBuffer=new StringBuffer("张三,李四,王五,赵六");

System.out.println("contentEquals:CharSequence    "+name.contentEquals(charSequence));//将此字符串与指定的 CharSequence比较。
System.out.println("contentEquals:StringBuffer    "+name.contentEquals(stringBuffer));//将此字符串与指定的 StringBuffer比较。

打印的结果:

contentEquals:CharSequence    true
contentEquals:StringBuffer    false

七:startsWith【测试此字符串是否以指定的前缀开始】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println("startsWith:"+name.startsWith("张"));//测试此字符串是否以指定的前缀开始。
System.out.println("startsWith():"+name.startsWith("李",3));//测试此字符串从指定索引开始的子字符串是否以指定前缀开始。

打印的结果:

startsWith:true
startsWith():true

八:endsWith【测试此字符串是否以指定的后缀结束】

案例:

String name="张三,李四,王五,赵六,赵七";
System.out.println("endsWith:"+name.endsWith("七"));

打印的结果:

endsWith:true

九:equals【将此字符串与指定的对象比较】和 equalsIgnoreCase【将此 String 与另一个 String 比较,不考虑大小写】

案例:

String b2="c";
tring b3="C";

System.out.println("equals:"+b2.equals(b3));//将此字符串与指定的对象比较。
System.out.println("equalsIgnoreCase:"+b2.equalsIgnoreCase(b3));//将此 String 与另一个 String 比较,不考虑大小写。

打印的结果:

equals:false
equalsIgnoreCase:true

十:indexOf【返回指定子字符串在此字符串中第一次出现处的索引】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println(name.indexOf(26446));//返回指定子字符串在此字符串中第一次出现处的索引。参数为Unicode 代码点
System.out.println(name.indexOf(26446,2));//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。参数为Unicode 代码点
System.out.println(name.indexOf("李"));//返回指定子字符串在此字符串中第一次出现处的索引。
System.out.println(name.indexOf("赵", 2));//返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

打印的结果:

3
3
3
9

十一:lastIndexOf【返回指定子字符串在此字符串中最后一次出现处的索引】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println(name.lastIndexOf(26446));//返回指定子字符串在此字符串中最后一次出现处的索引。参数为Unicode 代码点
System.out.println(name.lastIndexOf(26446,12));//返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。参数为Unicode 代码点
System.out.println(name.lastIndexOf("李"));//返回指定子字符串在此字符串中最后一次出现处的索引。
System.out.println(name.lastIndexOf("赵", 12));//返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。

打印的结果:

3
3
3
12

十二:isEmpty【判断String是否为空】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println(name.isEmpty());// 当且仅当 length() 为 0 时返回 true。

打印的结果:

false

十三:length【返回此字符串的长度】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println(name.length());

打印的结果:

14

十四:replace【替换】、replaceAll【替换所有】、replaceFirst【替换第一个】

案例:

String s = "my.test.txt";

System.out.println(s.replace(".", "#"));//返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
System.out.println(s.replaceAll(".", "#"));//使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 
System.out.println(s.replaceFirst(".", "#"));//使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

打印的结果:

my#test#txt
###########
#y.test.txt

十五:substring【截取字符串】和 subSequence【截取字符序列】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println(name.subSequence(2, 4));//返回一个新的字符序列,它是此序列的一个子序列。
System.out.println(name.substring(3));//返回一个新的字符串,它是此字符串的一个子字符串。
System.out.println(name.substring(3, 4));//返回一个新字符串,它是此字符串的一个子字符串。 

打印的结果:

,李
李四,王五,赵六,赵七

十六:split【分离字符串,详情见Java中split的用法

案例:

String name="张三,李四,王五,赵六,赵七";

String []asplit=name.split(",");

for (int i = 0; i < asplit.length; i++) {

System.out.println(asplit[i]);//根据给定正则表达式的匹配拆分此字符串。
}

打印的结果:

张三
李四
王五
赵六
赵七

十七:toCharArray【将此字符串转换为一个新的字符数组】

案例:

String name="张三,李四,王五,赵六,赵七";

char[] news;
news=name.toCharArray();
        for (int i = 0; i < news.length; i++) {
System.out.println(news[i]);
}

打印的结果:



,


,


,


,


十八:toLowerCase【大写转小写】和 toUpperCase【小写转大写】

案例:

String a2="A";
String b2="c";

System.out.println(a2.toLowerCase());// 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
System.out.println(b2.toUpperCase());//使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

打印的结果:

a
C

十九:trim【去除字符串前后空格】

案例:

String c="  a  b  ";

System.out.println(c.trim());//返回字符串的副本,忽略前导空白和尾部空白。

打印的结果:

a  b

二十:toString【返回此对象本身(它已经是一个字符串!)】

案例:

String name="张三,李四,王五,赵六,赵七";

System.out.println(name.toString());

打印的结果:

张三,李四,王五,赵六,赵七


String的方法有很多,以上是String常用的二十种方法。希望对大家有点帮助,对我自己也有点帮助。

String的其他方法【我就不一一举例了】:
format getBytes getChars offsetByCodePoints regionMatches valueOf hashCode codePointCount codePointBefore codePointAt intern matches copyValueOf









猜你喜欢

转载自blog.csdn.net/tian_tian2/article/details/77892569