**1.length() 获取字符串长度 **
String str="abc";
int n=str.length();
System.out.println(n)
2.charAt(int n) 定位n下标的字母
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWord";
System.out.println(a.charAt(2));
}
}
结果显示: l
3.tocharArray() 将字符串变成一个字符数组
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWord";
char[] Array = a.toCharArray();
System.out.println(Arrays.toString(Array));
}
}
输出结果:[H,e,l,l,o,W,o,r,d]
4.equals()和equalsIngoreCase() 比较两个字符串是否相等,前者区分大小写,后者不区分大小写
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWorld";
String b="helloworld";
boolean t=a.equals(b);
boolean h=a.equalsIgnoreCase(b);
System.out.println(t);
System.out.println(h);
}
}
输出结果: false true
5.startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWorld";
boolean t=a.startsWith("b");
boolean h=a.endsWith("d");
System.out.println(t);
System.out.println(h);
}
}
输出结果:false true
6.spilt() 分隔字符串
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWorld";
String[] s1=a.split("W");
System.out.println(s1[0]);
System.out.println(s1[1]);
}
}
输出结果:Hello orld
7.concat 连接字符串
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWorld";
String b="你好啊";
String s=b.concat(a);
System.out.println(s);
}
}
输出结果:你好啊HelloWorld
8.subString(int startindex,int endindex) 按初始下标和结束下标分隔字符串
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWorld";
String str1=a.substring(0,5);
String str2=a.substring(5,a.length());
System.out.println(str1);
System.out.println(str2);
}
}
输出结果:Hello World
9.replace() 替换字符串
public class TestDemo2 {
public static void main(String[] args) {
String a="HelloWorld";
String b=a.replace("lo","ph");
System.out.println(b);
}
}
输出结果:HelphWorld