使用可变字符串,我们可以更改现有对象的内容,这不会创建新对象。因此,可变字符串是那些可以在不创建新对象的情况下更改其内容的字符串。 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中文网其他相关文章!