📜  Java字符串之-length()

📅  最后修改于: 2020-09-26 05:56:27             🧑  作者: Mango

Java String length()

java 字符串 length()方法的字符串长度。它返回字符总数的计数。 Java 字符串的长度是相同的字符串的Unicode代码单元。

内部实现

public int length() {
return value.length;
}

Signature(签名)

字符串 length()方法的签名如下:

public int length()

指定者

CharSequence接口

返回值或类型

字符长度

Java String length()方法示例

public class LengthExample{
public static void main(String args[]){
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//10 is the length of javatpoint string
System.out.println("string length is: "+s2.length());//6 is the length of python string
}}
string length is: 10
string length is: 6

Java String length()方法示例2

public class LengthExample2 {
public static void main(String[] args) {
String str = "Javatpoint";
if(str.length()>0) {
System.out.println("String is not empty and length is: "+str.length());
}
str = "";
if(str.length()==0) {
System.out.println("String is empty now: "+str.length());
}
}
}
String is not empty and length is: 10
String is empty now: 0