眾所周知,java物件基本上有兩種類型,它們是可變的和不可變的。不可變物件是指一旦創建其內容就無法修改的物件。每當不可變物件的內容發生變更時,都會建立新物件。對於可變對象,我們可以更改現有對象的內容,但這不會導致創建新對象。因此,可變字串是那些可以在不建立新物件的情況下更改其內容的字串。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
StringBuffer和StringBuilder是java中String的可變版本,而java String類別是不可變的。因此 StringBuffer 是一個可變字串,用於為字串物件提供可變性。 String 類別包含固定長度、不可變的字元序列,而字串緩衝區具有可擴展的長度和可寫入的字元序列。
這裡有一些要點展示了我們如何在 java 中使用 StringBuffer。
以下是字串緩衝區建構子:
這是 StringBuffer 類別的聲明:
public final class StringBuffer extends Object implements Serializable,CharacterSequence,Appendable
現在我們將了解 StringBuffer 中可用的不同方法和欄位。以下是 StringBuffer 類別中常用方法的列表:
方法名稱 | 描述 |
長度()和容量() | 可變字串的長度可以使用length()方法計算,對應的容量可以使用capacity()計算。 |
追加(字串str)
追加(整數) |
此方法用於在現有字串緩衝區的末尾新增文字。 |
插入(int索引,字串str)
插入(int索引,char ch) |
用於在給定字串的指定位置插入文字。在給定的語法中,索引指定要插入字串的起始索引。 |
反向() | 用於反轉給定字串緩衝區物件中的字元順序。 |
刪除(int start, int end)和deleteCharAt(int index) | 用於從字串緩衝區中刪除字元。 Start 表示要刪除的第一個字元的起始索引,end 表示要刪除的最後一個字元後面的索引。 |
替換(int startindex, int endindex, String str) | 用於用指定的字串緩衝區替換startindex和endindex-1之間的字元序列。 |
charAt(int 索引) | 用於傳回字串緩衝區中指定索引處的字元。 |
codePointAt(int 索引) | 用於傳回指定索引處字元的 Unicode。 |
codePointBefore(int 索引) | 用於傳回指定索引之前的字元的Unicode。 |
子字串(int start)
substring(int start, int end) |
Used to return a new String that contains a subsequence of characters contained in a given string. |
ensureCapacity(int capacity) | Used for increasing the capacity of an existing string buffer object. |
toString() | Used to convert mutable string buffer to an immutable string object. |
Here are some of the examples of StringBuffer class which are given below:
Let us see a basic example of the StringBuffer class.
Code:
public class StringBufferDemo{ public static void main(String args[]){ StringBuffer sBuffer1=new StringBuffer("Welcome"); System.out.println("Original String is ::: " + sBuffer1 + ":: having length " + sBuffer1.length()); //using append method sBuffer1.append(" To Edubca"); System.out.println("Modified String after append is :: " + sBuffer1 + " :: having length " + sBuffer1.length()); //using reverse method sBuffer1.reverse(); System.out.println("Modified String after Reverse is :: " + sBuffer1); } }
The above code shows the creation of java StringBuffer and its different methods. The following output will be produced.
Output:
In this example, we will see some more methods of the StringBuffer class.
Code:
public class StringBufferDemo{ public static void main(String args[]){ StringBuffer sBuffer=new StringBuffer ("WelcomeToEdubca"); System.out.println("Original String is ::: " + sBuffer + ":: having length " + sBuffer.length()); //using replace method sBuffer.replace(0,9,"This is "); System.out.println("Modified String after replace is :: " + sBuffer + " :: having length " + sBuffer.length()); //using delete method sBuffer.delete(0,7); System.out.println("Modified String after delete is :: " + sBuffer); } }
The above code will display the following as output.
Output:
In the above example, we have seen how to create a StringBuffer class and usage of its methods.
From the above discussion, we have a clear understanding of StringBuffer in java, how it is created, and the different methods available in the StringBuffer class. Also, StringBuffer is thread-safe; therefore, it can be used in a multithreading environment.
以上是Java中的StringBuffer類的詳細內容。更多資訊請關注PHP中文網其他相關文章!