Java String length() メソッド
length() メソッドは、文字列の長さを返すために使用されます。
空文字列の長さは 0 を返します。
構文
public int length()
パラメータ
- 无
戻り値
文字列の長さを返します。
例
以下の例は、length() メソッドの使用を示しています:
例
public class Test {
public static void main(String args[]) {
String Str1 = new String("www.example.com");
String Str2 = new String("サンプル" );
System.out.print(文字列 Str1 の長さ:);
System.out.println(Str1.length());
System.out.print(文字列 Str2 の長さ:);
System.out.println(Str2.length());
}
}
public static void main(String args[]) {
String Str1 = new String("www.example.com");
String Str2 = new String("サンプル" );
System.out.print(文字列 Str1 の長さ:);
System.out.println(Str1.length());
System.out.print(文字列 Str2 の長さ:);
System.out.println(Str2.length());
}
}
上記のプログラムの実行結果は次のとおりです:
字符串 Str1 长度 :14 字符串 Str2 长度 :6
その他の拡張
Java Stringクラス