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

Java String類