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
廣告