Java StringBuffer length() 方法



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

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

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

語法

以下是 Java StringBuffer length() 方法的語法:

public int length()

引數

  • 它不接受任何引數。

返回值

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

示例:獲取 StringBuffer 字串的長度

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

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

public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuffer
      StringBuffer sb = new StringBuffer("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

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

如果當前 StringBuffer 為空字串,則 length() 方法返回0

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

import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuffer
      StringBuffer sb = new StringBuffer("");
      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

示例:獲取 StringBuffer 字串的長度

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

public class Demo {
   public static void main(String[] args) {
      //creating an object of the StringBuffer
      StringBuffer sb = new StringBuffer("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

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

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

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

public class Demo {
   public static void main(String[] args) {
      //instantiating  StringBuffer
      StringBuffer sb = new StringBuffer("   ");
      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_stringbuffer.htm
廣告