String方法汇总
1.concat() 方法用于连接两个或多个字符串。
public static void main(String[] args) {
String str1 = "abc";
char a[] = {'n','i','c','e'};
String str2 = new String(a,1,3);
System.out.println(str2);//ice
String concat = str1.concat(str2);
System.out.println(concat);//abcice
}
输出结果:
2.contain() 判断一个字符串中是否包含某一个小串
String str = "abcdefghigk";
String str3 = "cde";
boolean contains = str.contains(str3);
System.out.println(contains);
3.starwith()判断这个字符串是否已另一个字符串开头
String kaishi = "qwertyuiop";
String tou = "qwer";
boolean shifou = kaishi.startsWith(tou);
System.out.println(shifou);
4.endwith()判断这个字符串是否已另一个字符串开头
String kaishi1 = "qwertyuiop";
String tou1 = "qwer";
boolean jieshu = kaishi1.endsWith(tou1);
System.out.println(jieshu);
5.equalsIgnoreCase()判断和另一个字符串的内容是否相同 (不区分大小写)
String bijiao = "qwertyuiop";
String yuan = "qwertyUIOP";
boolean panduandaxiaoxie = bijiao.equalsIgnoreCase(yuan);
System.out.println(panduandaxiaoxie);
6.byte[] getBytes() 把一个字符串变成一个字节数组
String zifuchuan = "qwertyuiop";
byte[] bytes = zifuchuan.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]+" ");
}//113 119 101 114 116 121 117 105 111 112
输出:
7.int indexof(int ch) 在字符串中查找某个字符第一次出现的索引
String suibian = "qweqqrtyuiop";
int diyici = suibian.indexOf("q");
System.out.println(diyici);//0
8.indexof(int ch,int fromindex) 从某个位置开始,某个字符第一次出现的索引
String yanzheng = "qweqqrtyuiop";
int chuxian = yanzheng.indexOf("q", 2);
System.out.println(chuxian);
9.int indexof(int ch) 在字符串中查找某个“小”字符第一次出现的索引
String suibian1 = "qweqqrtyuiop";
int diyici1 = suibian.indexOf("iop");
System.out.println(diyici1);
10.int lastindexof(int ch) 在字符串中倒着查找某个字符第一次出现的索引
String daozhe = "qweqqrtyuiop";
int uyt = daozhe.lastIndexOf("t");
System.out.println(uyt);
11.String replaceAll(String regex,String replacement) 在一个字符串中,用一个新的小串把老的小串替换掉
String shuzu11 = "qazwsxedcrfvtgbyhn";
String tihuan = shuzu11.replaceAll("qa", "oo");
System.out.println(tihuan);
输出:
12.replaceFirst(String regex,String replacement) 在一个字符串中,用一个新的小串,把第一个出现的老小串替换掉
String shuzu22 = "qazwsxedqacrfvtgbyhn";
String tihuan22 = shuzu22.replaceFirst("qa", "oo");
System.out.println(tihuan22);
输出:
13.String[] split(String regex) 切割字符串
String qiange = "qazws,x,edq,acrf,vtgby,hn";
String[] split = qiange.split(",");
for (int i = 0; i < split.length; i++) {
System.out.print(split[i]+" ");
}
输出:
14.String substring(int beginIndex) 截取字符串
String jiequ = "qazwsxedqacrfvtgbyhn";
String substring = jiequ.substring(1);
System.out.println(substring);
输出:
15.String substring(int beginIndex) 截取字符串 截取到指定位置
【提示】substring包头不包尾,得注意
String zhidingweizhi = "qazwsxedqacrfvtgbyhn";
String substring1 = zhidingweizhi.substring(4, 8);
System.out.println(substring1);
16.char[] toCharArray()把一个字符串转换为字符数组
String zifuchuan1 = "qazwsxedqacrfvtgbyhn";
char[] chars = zifuchuan1.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]+" ");
}
输出:
17.String toLowerCase() 把字符串转换为小写
String daxie = "QEWTYVBUBUABSUSNAUISYTASFUYDYwewqeqw";
String zhuan = daxie.toLowerCase();
System.out.println(zhuan);
输出:
18.String toUpperCase() 把字符串转换为大写
String daxie1 = "QEWTYVBUBUABSUSNAUISYTASFUYDYwewqeqw";
String s = daxie1.toUpperCase();
System.out.println(s);
输出:
19.String trim() 把字符窜两端的控制去除掉
【提示】只是去掉了前后的,中间的没效果切记哦!
String daxie2 = " W T Y B UB UA BSU S NA U I S YT A SF UY DY w ewq eqw ";
String trim = daxie2.trim();
System.out.println(trim);
输出:
20.static String valueof(char[] data)把任意的东西转换为字符串
char[] chs = {'q','a','y','o'};
String s1 = String.valueOf(chs);
System.out.println(s1);
输出: