Java length() 方法
length() 方法用於返回字符串的長度。
長度等於字符串中16 位Unicode 代碼單元的數量。
語法
public int length()
參數
- 無
返回值
返回字符串長度。
實例
public class Test { public static void main(String args[]) { String Str1 = new String("www.w3big.com"); String Str2 = new String("w3big" ); System.out.print("字符串 Str1 长度 :"); System.out.println(Str1.length()); System.out.print("字符串 Str2 长度 :"); System.out.println(Str2.length()); } }
以上程序執行結果為:
字符串 Str1 长度 :14 字符串 Str2 长度 :6