Java字符串length方法示例

时间:2020-02-23 14:35:14  来源:igfitidea点击:

字符串的length方法用于计算字符串的长度。
它返回字符串中的字符数。

方法签名:

public int length()

字符串长度示例:

package org.igi.theitroad;
 
public class StringLengthExample {
 /*
  * @Author: igi Mandliya
  */
 public static void main(String[] args) {
 
  //1st
  String str1 = "Hello world from theitroad.com";
  //2nd
  String str2 = "Core java programming tutorials";
  //3rd
  String str3 = "JAVA PROG内存MING TUTORIALS";
  //4th
  String str4="";
  
  System.out.println("str1's length : "+str1.length());
  System.out.println("str2's length : "+str2.length());
  System.out.println("str3's length : "+str3.length());
  System.out.println("str4's length : "+str4.length());
 }
 
}

运行上面的程序时,我们将得到以下输出:

str1's length : 30
str2's length : 31
str3's length : 26
str4's length : 0