1 length()字符串的長度
1 public static void main(String[] args) { 2 String str = "Good Boy"; 3 System.out.println(str.length()); 4 }
輸出的結果是字符串長度為8。
2 substring()截取字符串
1 public static void main(String[] args) { 2 String str = "Good Boy"; 3 System.out.println(str.substring(5)); 4 System.out.println(str.substring(0, 5)); 5 }
輸出的結果第一條為“Boy”,參數5(beginIndex)是開始截取的位置。
輸出的結果第二條是“Good (此處有一空格)” ,第一個參數0(beginIndex)是開始截取的位置,第二個參數5(endIndex)是截取結束的位置(含頭不含尾)。
3 equals()和equalsIgnoreCase()比較兩個字符串是否相等,前者區分大小寫,后者不區分大小寫
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 String b = "good boy"; 4 System.out.println(a.equals(b)); 5 System.out.println(a.equalsIgnoreCase(b)); 6 }
輸出的結果為第一條為false,第二條為true。
4 startsWith()和endsWith()判斷字符串是不是以特定的字符開頭或結束(區分大小寫)
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 String b = "good boy"; 4 System.out.println(a.startsWith("G")); 5 System.out.println(a.startsWith("g")); 6 System.out.println(a.startsWith("B", 5)); 7 System.out.println(b.endsWith("Y")); 8 System.out.println(b.endsWith("y")); 9 }
輸出結果依次為:
true
false
true
false
true
a.startsWith(String prefix,int toffset)用於判斷特定的下標是否為指定字符串中開始查找的位置
5 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出現的地方,后者是查找字符或字符串最后一次出現的地方
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 System.out.println(a.indexOf("o")); 4 System.out.println(a.lastIndexOf("o")); 5 }
輸出結果:
1
6
6 getchars()截取多個字符並由其他字符串接收
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 char[] b = new char[10]; 4 char[] c = new char[10]; 5 a.getChars(0, 3, b, 0); 6 a.getChars(0, 4, c, 0); 7 System.out.println(b); 8 System.out.println(c); 9 }
輸出的結果:
Goo
Good
其中第一個參數0是要截取的字符串的初始下標(int sourceStart),第二個參數是要截取的字符串的結束后的下一個下標(int sourceEnd)也就是實際截取到的下標是int sourceEnd-1,第三個參數是接收的字符串(char target[]),最后一個參數是接收的字符串開始接收的位置。
7 getBytes()將字符串變成一個byte數組
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 byte b [] = a.getBytes(); 4 System.out.println(new String(b)); 5 }
輸出的結果為Good Boy的byte數組。
8 toUpperCase()和toLowerCase()將字符串轉換為大寫或小寫
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 System.out.println(a.toLowerCase()); 4 System.out.println(a.toUpperCase()); 5 }
輸出的結果第一條為“good boy”,第二條為“GOOD BOY”。
9 concat() 連接兩個字符串
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 String b = "You Are A "; 4 System.out.println(b.concat(a)); 5 }
輸出的結果為“You Are A Good Boy”。
10 trim()去掉起始和結束的空格
1 public static void main(String[] args) { 2 String a = " Good Boy "; 3 System.out.println(a.trim()); 4 }
輸出的結果為“Good Boy”。
11 charAt()截取一個字符
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 System.out.println(a.charAt(1)); 4 }
輸出的結果是字符串a的下標為1的字符o。
12 toCharArray()將字符串變成一個字符數組
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 char b[] = a.toCharArray(); 4 System.out.println(b); 5 }
輸出結果:Good Boy
13 split 字符串分割
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 String b[] = a.split(" "); 4 System.out.println(b.length); 5 for (int i = 0; i < b.length; i++) { 6 System.out.println(b[i]); 7 } 8 }
輸出結果:
2
Good
Boy
14 replace() 替換
1 public static void main(String[] args) { 2 String a = "Good Boy"; 3 String b = "好孩子"; 4 System.out.println(a.replace(a, b)); 5 System.out.println(b.replace("好", "你是個")); 6 }
輸出的結果:
好孩子
你是個孩子