1.substring() 方法
返回字符串的子字符串
String s = "hello word";
String p = s.substring(1);
String p1 = s.substring(1,3);
System.out.println(p+"\n"+p1);
输出
ello word
el
2.trim() 方法
用于删除字符串的头尾空格
String s1 = " dabfie kdiad ";
String m = s1.trim();
System.out.println(m);
输出
dabfie kdiad
3.concat() 方法
用于将指定的字符串参数连接到字符串上
String s = "hello word";
String p = s.concat(": 你好");
System.out.println(p);
输出
hello word: 你好
4.replace()方法
将指定的字符或者字符串进行替换
String s = "hello word";
String p = s.replace("e","a");
String p1 = s.replace("el","la");
System.out.println(p+"\n"+p1);
输出
hallo word
hlelo word
5.replaceAll()方法
使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串
String Str = new String("www.taobao.com");
System.out.println(Str.replaceAll("(.*)google(.*)", "hello" ));
System.out.println(Str.replaceAll("(.*)taobao(.*)", "hello" ));
输出
www.taobao.com
hello
6.toUpperCase()和 s.toLowerCase()
转大小写
String s = "hello word";
String p = s.toUpperCase();
System.out.println(p+"\n"+p.toLowerCase());
输出
HELLO WORD
hello word
7.charAt()方法
获取单个字符
String s = "hello word";
char p = s.charAt(6);
System.out.println(p);
输出
w
8.toCharArray()方法
转字符数组
String s = "hello word";
char[] p = s.toCharArray();
for (int i = 0;i<p.length;i++){
System.out.print(p[i]+" ");
}
输出
h e l l o w o r d
9.endsWith()方法
判断以什么结尾
String s = "hello word";
Boolean b = s.endsWith("d");
System.out.println(b);
输出
true
10.contains()方法
判断字符串是否包含
String s = "hello word";
boolean b = s.contains("r");
System.out.println(b);
输出
true
11.split()方法
对字符串进行分割
String s = "hello word";
String[] p=s.split(" ");
for(int i=0 ;i<p.length;i++){
System.out.println(p[i]);
}
输出
hello
word