Java - String length() 方法



Java String length() 方法用于获取字符串的长度。长度是指字符串中字符的数量。length() 方法不接受任何参数,在获取字符串长度时不会抛出任何异常。

字符串是一个对象,它保存字符序列。java.lang.string 类表示 Java 中的字符串。Java 中的字符串是不可变的,创建后其值无法更改。

注意 - 字符串的长度只不过是给定字符串中存在的字符的数量,它也计算空格以及字符串字符。

语法

以下是 Java String length() 方法的语法 -

public int length()

参数

  • 它不接受任何参数。

返回值

此方法返回此对象表示的字符序列的长度。

获取字符串长度示例

如果给定的字符串不为空,则 length() 方法返回字符串的长度。

在下面的程序中,我们使用值 "HelloWorld" 创建一个字符串字面量。然后,使用 length() 方法,我们尝试检索当前字符串的长度。

package com.tutorialspoint;

public class Length {
   public static void main(String[] args) {
      
      //create a string literal
      String str = "HelloWorld";
      System.out.println("The given string is: " + str);
      
      //using the length method;
      int len = str.length();
      System.out.println("The lenght of the string is: " + len);
   }
}

输出

执行上述程序后,将产生以下结果 -

The given string is: HelloWorld
The length of the string is: 10

获取空字符串长度示例

如果给定的字符串值为 ,则此方法返回 0

在下面的示例中,我们使用空值创建 string 类的对象。使用 length() 方法,我们尝试获取当前字符串的字符串长度。

package com.tutorialspoint;

public class Length {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = "";// empty string
      System.out.println("The given string is: " + str);
      
      //using the length method;
      int len = str.length();
      System.out.println("The lenght of the string is: " + len);
   }
}

输出

以下是上述程序的输出 -

The given string is an empty. 
The length of the string is: 0

使用字符串长度比较较长字符串示例

使用 length() 方法,我们可以根据长度找到较长的字符串。

在此程序中,我们使用值 "java" 实例化 string 类。然后,我们使用值 "point" 创建一个字符串字面量。使用 length() 方法,我们尝试根据字符串长度查找较长的字符串。

package com.tutorialspoint;

public class Length {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("java");
      System.out.println("The first string is: " + str);
      String str2 = "point";
      System.out.println("The second string is: " + str2);
      
      //using the length method;
      int len1 = str.length();
      int len2 = str2.length();
      System.out.println("The length of the strings is: " + len1 + " and " + len2) ;
      if(len1 > len2) {
         System.out.println("The first string is larger than the second string");
      } else {
         System.out.println("The first string is smaller than the second string");
      }
   }
}

输出

上述程序产生以下输出 -

The first string is: java
The second string is: point
The length of the strings is: 4 and 5
The first string is smaller than the second string

检查空字符串长度时获取异常示例

如果给定的字符串值为 null,则 length() 方法会抛出 NullPointerException

在下面的程序中,我们声明一个字符串并将其初始化为 null 值。使用 length() 方法,我们尝试检索字符串长度。由于给定的字符串值为 null,因此此方法会抛出异常。

public class Length {
   public static void main(String[] args) {
      try {
         
         //declare a string
         String str;
         
         //initialize it with null value
         str = null;
         System.out.println("The given string is: " + str);
         
         //using the length() method
         System.out.println("The length of the string is: " + str.length());
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

输出

执行上述程序后,将产生以下输出 -

The given string is: null
java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
	at com.tutorialspoint.String.Length.main(Length.java:11)
Exception: java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
java_lang_string.htm
广告

© . All rights reserved.