Java StringBuilder length() 方法



Java StringBuilder length() 方法用於獲取 StringBuilder 物件的長度。長度只是字串/StringBuilder 中字元的數量。如果當前 StringBuilder 物件為空,則此方法返回零。

length() 方法不接受任何引數。在檢索字串長度時,它不會丟擲任何異常。

注意 - 如果字串是空字串但包含一些空格,則 length() 方法會計算空格並將其作為字串的長度返回。

語法

以下是 Java StringBuilder length() 方法的語法 -

public int length()

引數

  • 它不接受任何引數。

返回值

此方法返回此物件當前表示的字元序列的長度。

示例:獲取 StringBuilder 字串的長度

如果 StingBuilder 物件的值不為 null,則此方法返回字串的長度。

在以下示例中,我們使用值“TutorialsPoint”建立一個StringBuilder物件。使用length()方法,我們正在檢索當前 StringBuilder 物件的長度。

public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("TutorialsPoint");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

輸出

執行上述程式後,將產生以下結果 -

The given string is: TutorialsPoint
The length of the 'TutorialsPoint' is: 14

示例:獲取 StringBuilder 空字串的長度

如果當前 StringBuilder 是一個空字串,則 length() 方法返回0

在以下程式中,我們使用值例項化StringBuilder。使用length()方法,我們嘗試檢索此 StringBuilder 物件的長度。

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

輸出

以下是上述程式的輸出 -

The given string is: 
The length of the '' is: 0

示例:獲取 StringBuilder 字串的長度

在以下示例中,我們使用值“HelloWorld”建立一個StringBuilder物件。然後,我們使用 for 迴圈遍歷字串的length()。使用charAt()方法,我們嘗試列印當前物件的每個字元。

public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuilder
      StringBuilder sb = new StringBuilder("HelloWorld");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
      //using for loop
      System.out.print("The string characters are: ");
      for(int i = 0; i<length; i++) {
         //using the charAt() method
         System.out.print(sb.charAt(i) + " ");
      }
   }
}

輸出

上述程式產生以下結果 -

The given string is: HelloWorld
The length of the 'HelloWorld' is: 10
The string characters are: H e l l o W o r l d

示例:獲取 StringBuilder 包含空格的字串的長度

如果當前物件僅包含一些空格,則 length() 方法會計算空格並將其作為字串長度返回。

在以下程式中,我們使用空值例項化StringBuilder,但它包含4 個空格。使用length()方法,我們嘗試檢索其長度。

public class Demo {
   public static void main(String[] args) {
      //instantiating  StringBuilder
      StringBuilder sb = new StringBuilder("   ");
      System.out.println("The given string is: " + sb);
      //using the length() method
      int length = sb.length();
      System.out.println("The length of the '" + sb + "' is: " + length);
   }
}

輸出

執行上述程式後,將產生以下輸出 -

The given string is:
The length of the '   ' is: 3
java_lang_stringbuilder.htm
廣告