Java.lang.String.length() 方法
描述
java.lang.String.length() 方法返回这个字符串的长度。 长度等于字符串中 Unicode 代码单元的数量。
声明
以下是 java.lang.String.length() 方法的声明。
public int length()
参数
NA
返回值
此方法返回此对象表示的字符序列的长度。
异常
NA
示例
下面的例子展示了 java.lang.String.length() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str = "tutorials point";
// prints length of string
System.out.println("length of string = " + str.length());
// checks if the string is empty or not
System.out.println("is this string empty? = " + str.isEmpty());
// empty string
str = "";
// prints length of string
System.out.println("length of string = " + str.length());
// checks if the string is empty or not
System.out.println("is this string empty? = " + str.isEmpty());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
length of string = 15 is this string empty? = false length of string = 0 is this string empty? = true

