判斷功能相關(guān)的方法
- equals方法
- equalsIgnoreCase
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
//將此 String 與另一個 String 比較
System.out.println(s1.equals(s2));// true
System.out.println(s1.equals(s3));// false
// 將此 String 與另一個 String 比較榴啸,不考慮大小寫。
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
獲取功能的方法
String s = "helloworld";
// 獲取字符串的長度
System.out.println(s.length());
System.out.println("++++++++++++++++++++++++++");
// 將指定的字符串連接到該字符串的末尾
String s2 = s.concat("**haha");
System.out.println(s2);
System.out.println("++++++++++++++++++++++++++");
// 獲取指定索引的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("++++++++++++++++++++++++++");
// 返回目標(biāo)第一次出現(xiàn)的索引, 沒有返回-1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("a"));
System.out.println("++++++++++++++++++++++++++");
// 從start開始 截取字符串到字符串結(jié)尾的值
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("++++++++++++++++++++++++++");
// 從start開始到end結(jié)束的截取, 左閉右開
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3, 8)); // lowor
轉(zhuǎn)換功能的方法
String s = "helloworld";
// 轉(zhuǎn)化為字符數(shù)組
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
// 轉(zhuǎn)化為字節(jié)數(shù)組
byte[] bytes = s.getBytes();
for (int i = 0; i <bytes.length ; i++) {
System.out.println(bytes[i]);
}
// 替換
s = s.replace('h', 'H');
System.out.println(s);
s = s.replace("wo", "WO");
System.out.println(s);