字符串的长度是多少?

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