本文概述
- Java String length()方法示例
- Java String length()方法示例2
内部实施
public int length() {
return value.length;
}
签名
字符串length()方法的签名如下:
public int length()
指定者
CharSequence接口
退货
字符长度
Java String length()方法示例
public class LengthExample{
public static void main(String args[]){
String s1="srcmini";
String s2="python";
System.out.println("string length is: "+s1.length());
//10 is the length of srcmini string
System.out.println("string length is: "+s2.length());
//6 is the length of python string
}}
【Java String length()】立即测试
string length is: 10
string length is: 6
Java String length()方法示例2
public class LengthExample2 {
public static void main(String[] args) {
String str = "srcmini";
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
推荐阅读
- Java字符串replace()
- Java字符串lastIndexOf()
- Java字符串join()
- Java字符串isEmpty()
- Java字符串getChars()
- Java字符串getBytes()
- 情人节微信红包怎样发?情人节发520微信红包办法_微信
- 情人节微信红包发多少合适?情人节微信红包数字含义大全_微信
- 情人节微信黄金红包怎样发?微信黄金红包入口_微信