使用可變字串,我們可以更改現有物件的內容,這不會建立新物件。因此,可變字串是那些可以在不建立新物件的情況下更改其內容的字串。 StringBuffer和StringBuilder是java中String的可變版本,而java String類別是不可變的。不可變物件是指一旦創建其內容就無法修改的物件。每當不可變物件的內容發生變更時,都會建立一個新物件。
廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗如前所述,java 中的可變字串可以使用 StringBuffer 和 StringBuilder 類別建立。兩者之間的主要區別在於 StringBuffer 是線程安全的實現,而 StringBuilder 不是。每當需要高效能和高安全性時,人們應該會喜歡 String 類別的可變版本。由於String池的存在,String類別存在安全問題;因此,每當需要資料安全時,就會使用 StringBuffer 和 StringBuilder。 StringBuffer 在效能方面比 StringBuffer 更好,因為 StringBuffer 是線程安全的,但每當需要線程安全時,就應該選擇 StringBuffer。
以下是字串產生器和字串緩衝區類別的主要建構子。
以下是字串緩衝區建構子:
這是 StringBuffer 類別的聲明:
public final class StringBuffer extends Object implements Serializable,CharacterSequence
以下是字串建構子建構子:
這是 StringBuilder 類別的聲明:
public final class StringBuilder extends Object implements Serializable,CharacterSequence
現在我們將看到 StringBuffer 和 StringBuilder 類別中可用的不同方法和欄位。以下是其中可用的常用方法清單:
字串可變類別的方法如下:
Method Name | Description |
length() and capacity() | The length of a mutable string can be calculated using the length() method, and corresponding capacity can be calculated using capacity(). |
append(String str)
append(int number) |
This method is used for adding new text at the end of an existing mutable string. |
insert(int index, String str)
insert(int index, char ch) |
Used for inserting text at a specified position in a given string. In the given syntax index specifies the starting index of at which the string will be inserted. |
reverse() | Used for reversing the order of character in a given string. |
delete(int start, int end) and deleteCharAt(int index) | Used for deleting characters from a mutable string. Start indicates the starting index of the first character to be removed, and the end index indicates an index of one past the last character to be removed. |
replace(int startindex, int endindex, String str) | Used for replacing character sequence between startindex and endindex-1 with the specified string. |
The above-listed methods are commonly used methods of StringBuffer and StringBuilder classes.
Examples of mutable string in java are given below:
Let us see a basic example of a StringBuffer class.
Code:
package com.edubca.mutablestringdemo; public class MutableStringDemo{ 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 how to use StringBuilder in java.
Code:
package com.edubca.mutablestringdemo; public class MutableStringDemo{ public static void main(String args[]){ StringBuilder sBuilder=new StringBuilder("WelcomeToEdubca"); System.out.println("Original String is ::: " + sBuilder + ":: having length " + sBuilder.length()); //using replace method sBuilder.replace(0,9,"This is "); System.out.println("Modified String after replace is :: " + sBuilder + " :: having length " + sBuilder.length()); //using delete method sBuilder.delete(0,7); System.out.println("Modified String after delete is :: " + sBuilder); } }
Output:
In the above example, we have seen how to createStringBuilder class and usage of its methods.
以上是Java 中的可變字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!