📅  最后修改于: 2020-09-26 05:56:27             🧑  作者: Mango
java 字符串 length()方法的字符串长度。它返回字符总数的计数。 Java 字符串的长度是相同的字符串的Unicode代码单元。
public int length() {
return value.length;
}
字符串 length()方法的签名如下:
public int length()
CharSequence接口
字符长度
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
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