字串的長度是多少?

Java中如何求字串的長度?
Java 提供了一種簡單方便的方法來計算任何String的長度。話不多說,讓我們來看一個查找字串長度的範例。例子
public class StringLength {
public static void main(String args[]) {
String alphabetsWithSpaces = "A B C";
String digitsWithSpaces = "1 2 3";
String specialCharsWithSpaces = "! @ $";
String allChars = "Z X 3 $$$ ^^^ * )(";
String sentence = "Hey, it's finally summers!";
System.out.println(alphabetsWithSpaces + " length = " + alphabetsWithSpaces.length());
System.out.println(digitsWithSpaces + " length = " + digitsWithSpaces.length());
System.out.println(specialCharsWithSpaces + " length = " + specialCharsWithSpaces.length());
System.out.println(allChars + " length = " + allChars.length());
System.out.println(sentence + " length = " + sentence.length());
}
}
輸出
ABC 長度 = 5 1 2 3 長度 = 5 !@ $ length = 5 ZX 3 $$$ ^^^ * )( length = 23 嘿,終於到了夏天!length = 26
GO TO FULL VERSION