首頁  >  文章  >  Java  >  Java 中的可變字串

Java 中的可變字串

WBOY
WBOY原創
2024-08-30 15:41:02680瀏覽

使用可變字串,我們可以更改現有物件的內容,這不會建立新物件。因此,可變字串是那些可以在不建立新物件的情況下更改其內容的字串。 StringBuffer和StringBuilder是java中String的可變版本,而java String類別是不可變的。不可變物件是指一旦創建其內容就無法修改的物件。每當不可變物件的內容發生變更時,都會建立一個新物件。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

如何在 Java 中使用可變字串?

如前所述,java 中的可變字串可以使用 StringBuffer 和 StringBuilder 類別建立。兩者之間的主要區別在於 StringBuffer 是線程安全的實現,而 StringBuilder 不是。每當需要高效能和高安全性時,人們應該會喜歡 String 類別的可變版本。由於String池的存在,String類別存在安全問題;因此,每當需要資料安全時,就會使用 StringBuffer 和 StringBuilder。 StringBuffer 在效能方面比 StringBuffer 更好,因為 StringBuffer 是線程安全的,但每當需要線程安全時,就應該選擇 StringBuffer。

StringBuilder 和 StringBuffer 類別的建構子

以下是字串產生器和字串緩衝區類別的主要建構子。

1. StringBuffer 建構子

以下是字串緩衝區建構子:

  • StringBuffer(): 這會建立一個空的 StringBuffer,預設容量為 16 個字元。
  • StringBuffer(intcapacity): 這將會建立一個具有指定容量的空 StringBuffer。
  • StringBuffer(CharSequence charseq): 這將建立包含與指定字元序列中相同字元的 StringBuffer。
  • StringBuffer(String str): 建立與指定String對應的StringBuffer。

這是 StringBuffer 類別的聲明:

public final class StringBuffer extends Object implements Serializable,CharacterSequence

2. StringBuilder 建構子

以下是字串建構子建構子:

  • StringBuilder(): 這會建立一個空的 StringBuilder,預設容量為 16 個字元。
  • StringBuilder(intcapacity): 這將建立一個具有指定容量的空 StringBuilder。
  • StringBuilder(CharSequence charseq): 這將建立包含與指定字元序列中相同字元的 StringBuilder。
  • StringBuilder(String str): 建立與指定 String 對應的 StringBuilder。

這是 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.
方法名稱 描述 長度()和容量() 可以使用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之間的字元序列替換為指定字串。 表>

The above-listed methods are commonly used methods of StringBuffer and StringBuilder classes.

Examples of Mutable String in Java

Examples of mutable string in java are given below:

Example #1

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:

Java 中的可變字串

Example #2

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:

Java 中的可變字串

In the above example, we have seen how to createStringBuilder class and usage of its methods.

以上是Java 中的可變字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java 計算機下一篇:Java 計算機