Java String 字符串

equals
字符串比较

String str = "furong";
String str1 = new String("furong");
System.out.println(str.equals(str1));
true

length

String str = "furong";
System.out.println(str.length());
6

toCharArray
转换成数组

String str = "furong";
char data[] = str.toCharArray();
for(int i = 0; i < str.length(); i++){
    System.out.print(data[i] + " ");
}
System.out.println();
f u r o n g 

charAt
取出指定位置字符

String str = "furong";
System.out.println(str.charAt(4));
n

indexOf
指定字符位置

String str = "furong@QUANGE";
System.out.println(str.indexOf('@'));
6

trim
去掉字符串前后空格

String str = "  furong@QUANGE";
System.out.println(str.indexOf('@'));
furong@QUANGE

substring
取出指定字符串

String str = "furong@QUANGE";
System.out.println(str.substring(3, 9));
ong@QU

toLowerCase
小写转换

String str = "furong@QUANGE";
System.out.println(str.toLowerCase());
furong@quange

toUpperCase
大写转换

String str = "furong@QUANGE";
System.out.println(str.toUpperCase());
FURONG@QUANGE

startsWith
判断开头字符

String str = "furong";
System.out.println(str.startsWith("f"));
true

endsWith
判断结尾字符

String str = "furong";
System.out.println(str.endsWith("g"));
true

replace
字符替换

String str = "furong@QUANGE";
System.out.println(str.replace('@', '#'));
furong#QUANGE

猜你喜欢

转载自blog.csdn.net/zhangxuechao_/article/details/80666309