Java 简明教程
Java StringBuilder Class
StringBuilder Class Introduction
Java StringBuilder 类是一个可变的字符序列。这提供了一个与 StringBuffer 兼容的 API,但没有同步保证。StringBuilder 类可用于替换只需线程操作的 StringBuffer。由于 StringBuffer 具有同步开销,因此 StringBuilder 作为 StringBuffer 的更快替代品。如果在需要同步字符串操作的多线程环境中,建议使用 StringBuffer,否则使用 StringBuilder 来提高性能。
The Java StringBuilder class is mutable sequence of characters. This provides an API compatible with StringBuffer, but with no guarantee of synchronization. StringBuilder class can be used to replace StringBuffer where single threaded operations are in use. As StringBuffer has overhead of synchronization, StringBuilder acts as a faster alternative of StringBuffer. In case of multi-threaded environment, where we need to synchronized string manipulation operations, StringBuffer is recommanded, otherwise StringBuilder is to be used to improve performance.
StringBuilder Class Declaration
以下是 java.lang.StringBuilder 类的声明 −
Following is the declaration for java.lang.StringBuilder class −
public final class StringBuilder
extends Object
implements Serializable, CharSequence
StringBuilder Class Constructors
以下是 StringBuilder 类的构造函数列表。
Following is the list of constructors of the StringBuilder class.
Sr.No. |
Constructor & Description |
1 |
StringBuilder() This constructs a string builder with no characters in it and an initial capacity of 16 characters. |
2 |
StringBuilder(CharSequence seq) This constructs a string builder that contains the same characters as the specified CharSequence. |
3 |
StringBuilder(int capacity) This constructs a string builder with no characters in it and an initial capacity specified by the capacity argument. |
4 |
StringBuilder(String str) This constructs a string builder initialized to the contents of the specified string. |
StringBuilder Class methods
以下是 StringBuilder 类的方法列表。每个方法都有多个示例,展示它的功能。
Following is the list of methods of the StringBuilder class. Each method is having multiple examples to demonstrate the functionality of the method.
Sr.No. |
Method & Description |
1 |
StringBuilder append()This method appends the given string argument to the sequence. |
2 |
StringBuilder 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 |
StringBuilder delete(int start, int end)This method removes the characters in a substring of this sequence. |
9 |
StringBuilder 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 |
StringBuilder 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 |
StringBuilder 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 |
StringBuilder 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 StringBuilder
以下示例说明了 Java StringBuilder append(Boolean b) 方法的用法。在此,我们通过字符串名称“tuts”实例化一个 StringBuilder 对象“buff”。然后,我们使用带有布尔类型参数“true”的“buff”对象调用 append() 方法。返回值将是附加的字符串名称“tuts true”。同样,我们使用字符串名称“abcd”和布尔类型参数“false”展示另一个案例。
The following example shows the usage of Java StringBuilder append(Boolean b) method. Here, we are instantiating a StringBuilder 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 StringBuilderDemo {
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder("tuts ");
System.out.println("builder = " + stringBuilder);
// appends the boolean argument as string to the string stringBuilder
stringBuilder.append(true);
// print the string stringBuilder after appending
System.out.println("After append = " + stringBuilder);
stringBuilder = new StringBuilder("abcd ");
System.out.println("stringBuilder = " + stringBuilder);
// appends the boolean argument as string to the string stringBuilder
stringBuilder.append(false);
// print the string stringBuilder after appending
System.out.println("After append = " + stringBuilder);
}
}