在Java中,String.length()方法用于返回字符串的長度,即字符串中包含的字符數。它是一個實例方法,需要通過一個String對象調用。
示例:
String str = "Hello, World!"; int length = str.length(); System.out.println("Length of the string: " + length);
輸出結果為:
Length of the string: 13