Java中String中类的常用方法
1)publicString(char[]value):将字符数组转成字符串;
charc[]={‘f’,’w’,’s’,’w’,’g’,’a’};
Strings=newString(c);
System.out.println(s);
结果为:fwswga
2)publicString(cahr[]value,开始位置,新字符串的长度):将指定位置长度的字符数组转成字符串;
charc[]={‘f’,’w’,’s’,’w’,’g’,’a’}
Strings=newString(c,2,2);
System.out.println(c);
结果为:sw
3)publicString(byte[]value):将字节数组转成字符串;
byteb[]={‘w’,’o’,’r’,’l’,’d’,’!’};
Strings=newString(b);
System.out.println(s);
结果为:world!
4)publicString(byte[]value,开始位置,新字符串长度);将指定位置长度的字节数组转成字符串;
byteb[]={‘w’,’o’,’r’,’l’,’d’,’!’};
Strings=newString(b,2,3);
System.out.println(s);
结果为:rl
5)publicchar[]toCharArray();将字符数组转成字符串
Strings=”Hello”;
Charc[]=s.toCharArray();
for(inti=0;i<c.length;i++)
{
System.out.print(c[i]+”\t”);}
结果为:H e l l o
6)publiccharcharAt(intindex字符下标):返回字符串中指定位置的字符
Strings=”Hello”;
System.out.println(s.charAt(3));
结果为:l
7)publicbyte()getBytes():将一个字符串转成字节数组,默认输出值为ASCII码值
Strings=”Hello”;
byteb[]=s.getBytes();
for(inti=0;i<b.length;i++)
{
System.out.print((char)b[i]+”\t”);
}
结果为:H e l l o
8)publicintlength():求字符串的长度
Strings=”dwfsdfwfsadf”;
System.out.pritnln(s.length());
结果为:12
9)publicintindexOf(Stringstr):从头查找指定的字符串的位置,返回值为整型,从0开始,如果没找到,则返回-1
Strings=”sdfwefsdgwe”;
System.out.println(s.indexOf(“e”));
结果为:4
10)publicintindexOf(Stringstr,指定位置);从指定位置开始查找指定的字符串
Strings=”dfwfgasdfwf”;
System.out.println(s.indexOf(“a”,8));
结果为:-1
11)publicStringtrim():清除字符串左右两端的空格
Strings=”sdfsdfs”;
System.out.println(s.trim);
结果为:sdfsdfs
12)publicStringsubstring(intbeginindex):从指定位置到结尾截取字符串
Strings=”fjeiflsjfowjflsdfjo”;
System.out.println(s.substring(5));
结果为:lsjfowjflsdfjo
13)publicStringsubstring(intbegin,intend);指定截取字符串的开始和结束位置,不包含结束字符
Strings=”foweflsdfjowefjls”;
System.out.println(s.substring(4,9));
结果为:flsdf
14)publicString[]split(Stringstr);按指定的字符分割字符串
Strings=”fwefsdfefgefaselieffs”;
Stringss[]=s.split(“e”);
for(inti=0;i<ss.length;i++)
{
System.out.print(ss[i]+”\t”);
}
System.out.println();
结果为:fw fsdf fg fas li ffs
15)publicStringtoUpperCase():将字符串全部转换成大写
System.out.println(newString(“hello”).toUpperCase());
结果为:HELLO
16)publicStringtoLowerCase();将字符全部转成小写
System.out.println(newString(“HELLO”).toLowerCase());
结果为:hello
17)publicbooleanstartsWith(Stringstr);判断字符串是否由指定的字符串开头
Strings=”fwofsdfjwkfs”;
System.out.println(s.startsWith(“fw”));
System.out.println(s.startsWith(“wf”));
结果为:true
false
18)publicbooleanendsWith(Stringstr):判断字符串是否由指定的字符串结尾
Strings=”fwefsdgjgrg”;
System.out.println(s.endsWith(“fe”));
System.out.println(s.endsWith(“rg”));
结果为:false
True
19publicbooleanequals(Stringstr):判断两个字符串是否相等,区分大小写
Strings1=”hello”;
Strings2=newString(“hello”);
Strings3=”Hello”;
System.out.println(“s1==s2—à”+(s1.equals(s2)));
System.out.println(“s1==s3—à”+(s1.equals(s3)));
结果为:true
False
20)publicbooleanequalsIgnoreCase(Stringstr):不区分大小写判断俩个字符串是否相等
Strings1=”hello”;
Strings2=”HEllo”;
System.out.println(“s1==s2–à”+(s1.equalsIgnoreCase(s2)));
结果为:true
21)publicStringreplaceAll(Stringstr1,Stringstr2);将字符串中str1替换成str2
Strings=”geowfjsklgoasdf”;
System.out.println(s.replaceAll(“f”,”s”));
结果为:geowsjsklgoasds
积极的人在每一次忧患中都看到一个机会,