众所周知,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中文网其他相关文章!