Java 简明教程
Java StringBuffer Class
StringBuffer Class Introduction
Java StringBuffer 类是可以变异的序列字符。通过 StringBuffer 可以轻松修改 String 内容。它提供了多种实用函数来处理字符串。一个 StringBuffer 的操作本质上是同步的,因此建议在多线程环境中使用。如果不需要同步,我们可以选择一个可选 API StringBuffer。
The Java StringBuffer class is mutable sequence of characters. StringBuffer can be used to modify the content of a String with ease. It provides many utility functions to manipulate a string. A StringBuffer opearations are syncronized in nature and it is recommanded to be used in multi-threading environment. In case, syncronization is not needed, we can go for an alternate API StringBuffer.
StringBuffer Class Declaration
下面是 java.lang.StringBuffer 类的声明:
Following is the declaration for java.lang.StringBuffer class −
public final class StringBuffer
extends Object
implements Serializable, CharSequence
StringBuffer Class Constructors
下面是 StringBuffer 类的构造函数列表:
Following is the list of constructors of the StringBuffer class.
Sr.No. |
Constructor & Description |
1 |
StringBuffer() This constructs a string builder with no characters in it and an initial capacity of 16 characters. |
2 |
StringBuffer(CharSequence seq) This constructs a string builder that contains the same characters as the specified CharSequence. |
3 |
StringBuffer(int capacity) This constructs a string builder with no characters in it and an initial capacity specified by the capacity argument. |
4 |
StringBuffer(String str) This constructs a string builder initialized to the contents of the specified string. |
StringBuffer Class methods
下面是字符串缓冲器类的各个方法。每个方法将拥有多个范例来展示方法的功能。
Following is the list of methods of the StringBuffer class. Each method is having multiple examples to demonstrate the functionality of the method.
Sr.No. |
Method & Description |
1 |
StringBuffer append()This method appends the given string argument to the sequence. |
2 |
StringBuffer appendCodePoint(int codePoint)This method appends the string representation of the codePoint argument to this sequence. |
3 |
int capacity()This method returns the current capacity. |
4 |
char charAt(int index)This method returns the char value in this sequence at the specified index. |
5 |
int codePointAt(int index)This method returns the character (Unicode code point) at the specified index. |
6 |
int codePointBefore(int index)This method returns the character (Unicode code point) before the specified index. |
7 |
int codePointCount(int beginIndex, int endIndex)This method returns the number of Unicode code points in the specified text range of this sequence. |
8 |
StringBuffer delete(int start, int end)This method removes the characters in a substring of this sequence. |
9 |
StringBuffer deleteCharAt(int index)This method removes the char at the specified position in this sequence. |
10 |
void ensureCapacity(int minimumCapacity)This method ensures that the capacity is at least equal to the specified minimum. |
11 |
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Characters are copied from this sequence into the destination character array dst. |
12 |
int indexOf(String str)This method returns the index within this string of the first occurrence of the specified substring. |
13 |
StringBuffer insert()This method inserts the string representation of the given argument into this sequence. |
14 |
int lastIndexOf(String str)This method returns the index within this string of the rightmost occurrence of the specified substring. |
15 |
int length()This method returns the length (character count). |
16 |
int offsetByCodePoints(int index, int codePointOffset)This method returns the index within this sequence that is offset from the given index by codePointOffset code points. |
17 |
StringBuffer replace(int start, int end, String str)This method replaces the characters in a substring of this sequence with characters in the specified String. |
18 |
StringBuffer reverse()This method causes this character sequence to be replaced by the reverse of the sequence. |
19 |
void setCharAt(int index, char ch)Character at the specified index is set to ch. |
20 |
void setLength(int newLength)This method sets the length of the character sequence. |
21 |
CharSequence subSequence(int start, int end)This method returns a new character sequence that is a subsequence of this sequence. |
22 |
String substring(int start)This method returns a new String that contains a subsequence of characters currently contained in this character sequence. |
23 |
String toString()This method returns a string representing the data in this sequence. |
24 |
void trimToSize()This method attempts to reduce storage used for the character sequence. |
Methods inherited
此类从以下类中继承方法:
This class inherits methods from the following classes −
-
java.lang.Object
-
java.lang.CharSequence
Example: Append a Boolean to the StringBuffer
以下示例演示了 Java StringBuffer append(Boolean b) 方法的使用。此处,我们使用字符串名称“tuts”实例化一个 StringBuffer 对象“buff”。然后,我们使用“buff”对象和布尔参数“true”调用 append() 方法。返回值为附加的字符串名称“tuts true”。同样,我们使用字符串名称“abcd”和布尔参数“false”演示另一个案例。
The following example shows the usage of Java StringBuffer append(Boolean b) method. Here, we are instantiating a StringBuffer object "buff" with the string name "tuts". Then, we invoke the append() method using the "buff" object with a boolean argument "true". The return value will be the appended string name "tuts true". Similarly, we demonstrate another case using the string name "abcd" and a boolean argument "false".
package com.tutorialspoint;
public class StringBufferDemo {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("tuts ");
System.out.println("buffer = " + stringBuffer);
// appends the boolean argument as string to the string stringBuffer
stringBuffer.append(true);
// print the string stringBuffer after appending
System.out.println("After append = " + stringBuffer);
stringBuffer = new StringBuffer("abcd ");
System.out.println("stringBuffer = " + stringBuffer);
// appends the boolean argument as string to the string stringBuffer
stringBuffer.append(false);
// print the string stringBuffer after appending
System.out.println("After append = " + stringBuffer);
}
}