Java String length()

本文概述

  • Java String length()方法示例
  • Java String length()方法示例2
java字符串的length()方法的字符串长度。它返回字符总数的计数。 Java字符串的长度与字符串的Unicode代码单元相同。
内部实施
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

    推荐阅读