文字列の長さはどれくらいですか?

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