Most visited

Recently visited

Added in API level 1

StringBuilder

public final class StringBuilder
extends Object implements CharSequence, Serializable, Appendable, CharSequence

java.lang.Object
   ↳ java.lang.StringBuilder


一个可变的字符序列。 该类提供了与StringBuffer兼容的API,但不保证同步。 这个类被设计用作在单个线程正在使用字符串缓冲区的地方(如通常情况下)的StringBuffer的插入式替换。 在可能的情况下,建议将此类优先用于StringBuffer因为在大多数实现中它会更快。

StringBuilder上的主要操作是appendinsert方法,它们被重载以便接受任何类型的数据。 每个函数都有效地将给定的数据转换为字符串,然后将该字符串的字符追加或插入到字符串构建器中。 append方法总是在构建器的末尾添加这些字符; insert方法在指定的点添加字符。

例如,如果 z引用当前内容为“ start ”的字符串构建器对象,则方法调用 z.append("le")将导致字符串构建器包含“ startle ”,而 z.insert(4, "le")会将字符串构建器更改为包含“ starlet ”。

一般情况下,如果某人是指的一个实例StringBuilder ,然后sb.append(x)具有相同的效果sb.insert(sb.length(), x) 每个字符串生成器都有一个容量。 只要包含在字符串构建器中的字符序列的长度不超过容量,就不需要分配新的内部缓冲区。 如果内部缓冲区溢出,它会自动变大。

StringBuilder实例不适合多线程使用。 如果需要这种同步,则建议使用StringBuffer

也可以看看:

Summary

Public constructors

StringBuilder()

构造一个没有字符的字符串生成器,初始容量为16个字符。

StringBuilder(int capacity)

构造一个字符串构建器,其中不包含任何字符,并由 capacity参数指定初始容量。

StringBuilder(String str)

构造一个字符串生成器,初始化为指定字符串的内容。

StringBuilder(CharSequence seq)

构造一个字符串构建器,其中包含与指定的 CharSequence相同的字符。

Public methods

StringBuilder append(boolean b)

boolean参数的字符串表示形式追加到序列中。

StringBuilder append(long lng)

long参数的字符串表示形式追加到该序列中。

StringBuilder append(char c)

char参数的字符串表示追加到该序列。

StringBuilder append(Object obj)

附加 Object参数的字符串表示形式。

StringBuilder append(char[] str, int offset, int len)

char数组参数的子数组的字符串表示追加到该序列。

StringBuilder append(double d)

double参数的字符串表示追加到此序列。

StringBuilder append(char[] str)

char数组参数的字符串表示追加到此序列。

StringBuilder append(String str)

将指定的字符串附加到此字符序列。

StringBuilder append(StringBuffer sb)

将指定的 StringBuffer附加到该序列。

StringBuilder append(float f)

float参数的字符串表示追加到此序列。

StringBuilder append(int i)

int参数的字符串表示追加到该序列。

StringBuilder append(CharSequence s, int start, int end)

将指定的 CharSequence序列 CharSequence到该序列。

StringBuilder append(CharSequence s)
StringBuilder appendCodePoint(int codePoint)

codePoint参数的字符串表示追加到该序列。

int capacity()

返回当前容量。

char charAt(int index)

以指定索引的顺序返回 char值。

int codePointAt(int index)

返回指定索引处的字符(Unicode码点)。

int codePointBefore(int index)

返回指定索引之前的字符(Unicode码点)。

int codePointCount(int beginIndex, int endIndex)

返回此序列的指定文本范围内的Unicode代码点数。

StringBuilder delete(int start, int end)

删除此序列的子字符串中的字符。

StringBuilder deleteCharAt(int index)

按照此顺序删除指定位置的 char

void ensureCapacity(int minimumCapacity)

确保容量至少等于规定的最小值。

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

将字符从该序列复制到目标字符数组 dst

int indexOf(String str)

返回指定子字符串第一次出现的此字符串中的索引。

int indexOf(String str, int fromIndex)

从指定索引处开始,返回指定子字符串第一次出现的此字符串中的索引。

StringBuilder insert(int offset, char[] str)

char数组参数的字符串表示形式插入到此序列中。

StringBuilder insert(int offset, float f)

float参数的字符串表示插入到该序列中。

StringBuilder insert(int dstOffset, CharSequence s)

将指定的 CharSequence插入此序列中。

StringBuilder insert(int offset, char c)

char参数的字符串表示插入到该序列中。

StringBuilder insert(int offset, long l)

long参数的字符串表示插入到该序列中。

StringBuilder insert(int index, char[] str, int offset, int len)

str数组参数的子数组的字符串表示插入到此序列中。

StringBuilder insert(int offset, int i)

将第二 int参数 int的字符串表示插入到该序列中。

StringBuilder insert(int offset, String str)

将字符串插入到此字符序列中。

StringBuilder insert(int offset, double d)

double参数的字符串表示插入到此序列中。

StringBuilder insert(int dstOffset, CharSequence s, int start, int end)

将指定的 CharSequence序列插入到该序列中。

StringBuilder insert(int offset, Object obj)

Object参数的字符串表示形式插入到此字符序列中。

StringBuilder insert(int offset, boolean b)

boolean参数的字符串表示插入到此序列中。

int lastIndexOf(String str, int fromIndex)

返回指定子字符串最后一次出现的此字符串中的索引。

int lastIndexOf(String str)

返回指定子字符串最右边出现的字符串中的索引。

int length()

返回长度(字符数)。

int offsetByCodePoints(int index, int codePointOffset)

返回此序列,其从给定的偏移处的索引 index通过 codePointOffset代码点。

StringBuilder replace(int start, int end, String str)

用指定的 String的字符替换此序列的子字符串中的字符。

StringBuilder reverse()

导致该字符序列被序列的反向替换。

void setCharAt(int index, char ch)

指定索引处的字符设置为 ch

void setLength(int newLength)

设置字符序列的长度。

CharSequence subSequence(int start, int end)

返回一个新的字符序列,该序列是该序列的子序列。

String substring(int start, int end)

返回一个新的 String ,其中包含当前包含在此序列中的字符的子序列。

String substring(int start)

返回一个新的 String ,其中包含当前包含在此字符序列中的字符的子序列。

String toString()

返回表示此序列中数据的字符串。

void trimToSize()

尝试减少用于字符序列的存储空间。

Inherited methods

From class java.lang.Object
From interface java.lang.CharSequence
From interface java.lang.Appendable

Public constructors

StringBuilder

Added in API level 1
StringBuilder ()

构造一个没有字符的字符串生成器,初始容量为16个字符。

StringBuilder

Added in API level 1
StringBuilder (int capacity)

构造一个字符串构建器,其中不含任何字符,并由 capacity参数指定初始容量。

Parameters
capacity int: the initial capacity.
Throws
NegativeArraySizeException if the capacity argument is less than 0.

StringBuilder

Added in API level 1
StringBuilder (String str)

构造一个字符串生成器,初始化为指定字符串的内容。 字符串构建器的初始容量是16加上字符串参数的长度。

Parameters
str String: the initial contents of the buffer.
Throws
NullPointerException if str is null

StringBuilder

Added in API level 1
StringBuilder (CharSequence seq)

构造一个字符串构建器,其中包含与指定的CharSequence相同的字符。 字符串构建器的初始容量是16加上CharSequence参数的长度。

Parameters
seq CharSequence: the sequence to copy.
Throws
NullPointerException if seq is null

Public methods

append

Added in API level 1
StringBuilder append (boolean b)

boolean参数的字符串表示追加到序列。

总体效果就好像通过方法 valueOf(boolean)将参数转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
b boolean: a boolean.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (long lng)

long参数的字符串表示追加到该序列。

整体效果就好像通过方法 valueOf(long)将参数转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
lng long: a long.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (char c)

char参数的字符串表示追加到该序列。

该参数被附加到该序列的内容。 这个序列的长度增加1

整体效果就好像参数通过方法 valueOf(char)转换为字符串,并且该字符串中的字符然后是该字符序列的 ERROR(appended/#append(String) appended)

Parameters
c char: a char.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (Object obj)

追加 Object参数的字符串表示形式。

总体效果就好像通过方法 valueOf(Object)将参数转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
obj Object: an Object.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (char[] str, 
                int offset, 
                int len)

char数组参数的子数组的字符串表示追加到此序列。

从索引offset开始的char数组str字符按顺序附加到该序列的内容。 该序列的长度增加了len的值。

总体效果就好像通过方法 valueOf(char[], int, int)将参数转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
str char: the characters to be appended.
offset int: the index of the first char to append.
len int: the number of chars to append.
Returns
StringBuilder a reference to this object.
Throws
IndexOutOfBoundsException

append

Added in API level 1
StringBuilder append (double d)

double参数的字符串表示追加到此序列。

总体效果就好像参数通过方法 valueOf(double)转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
d double: a double.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (char[] str)

char数组参数的字符串表示追加到该序列。

数组参数的字符按顺序附加到该序列的内容。 这个序列的长度增加了参数的长度。

总体效果就好像参数被方法 valueOf(char[])转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
str char: the characters to be appended.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (String str)

将指定的字符串附加到此字符序列。

String参数的字符按顺序追加,按顺序将该序列的长度增加参数的长度。 如果strnull ,则附加四个字符"null"

n是在执行append方法之前的这个字符序列的长度。 那么如果k小于n ,则新字符序列中索引k处的字符等于旧字符序列中索引k处的字符; 否则,它等于参数str中指数kn处的字符。

Parameters
str String: a string.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (StringBuffer sb)

追加指定的 StringBuffer到这个序列。

StringBuffer参数的字符按顺序附加到该序列中,并将该序列的长度增加参数的长度。 如果sbnull ,则将四个字符"null"附加到该序列。

n是执行append方法之前的这个字符序列的长度。 那么如果k小于n ,则新字符序列中索引k处的字符等于旧字符序列中索引k处的字符; 否则,它等于参数sb中指数kn处的字符。

Parameters
sb StringBuffer: the StringBuffer to append.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (float f)

float参数的字符串表示追加到此序列。

整体效果就好像参数被方法 valueOf(float)转换为字符串,并且该字符串的字符然后是 ERROR(appended/#append(String) appended)到该字符序列。

Parameters
f float: a float.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (int i)

int参数的字符串表示追加到该序列。

整体效果就好像参数通过方法 valueOf(int)转换为字符串,然后该字符串的字符对该字符序列为 ERROR(appended/#append(String) appended)

Parameters
i int: an int.
Returns
StringBuilder a reference to this object.

append

Added in API level 1
StringBuilder append (CharSequence s, 
                int start, 
                int end)

将指定的 CharSequence序列 CharSequence到该序列。

从索引start开始的参数s字符按顺序附加到该序列的内容直到(独占)索引end 该序列的长度增加了end - start的值。

假设n是在执行append方法之前该字符序列的长度。 然后在该字符序列索引k处的字符变为等于在索引k此序列中的字符,如果k小于n; 否则,它等于参数s中索引k + start-n处的s

如果 snull ,那么此方法会附加字符,就像s参数是包含四个字符 "null"的序列 "null"

Parameters
s CharSequence: the sequence to append.
start int: the starting index of the subsequence to be appended.
end int: the end index of the subsequence to be appended.
Returns
StringBuilder a reference to this object.
Throws
IndexOutOfBoundsException

append

Added in API level 1
StringBuilder append (CharSequence s)

Parameters
s CharSequence
Returns
StringBuilder

appendCodePoint

Added in API level 1
StringBuilder appendCodePoint (int codePoint)

codePoint参数的字符串表示追加到该序列。

该参数被附加到该序列的内容。 这个序列的长度增加Character.charCount(codePoint)

整体效果就好像参数通过方法 toChars(int)转换为 char数组,然后该数组中的字符对此字符序列为 ERROR(appended/#append(char[]) appended)

Parameters
codePoint int: a Unicode code point
Returns
StringBuilder a reference to this object.

capacity

Added in API level 1
int capacity ()

返回当前容量。 容量是可用于新插入字符的存储量,超出该容量将进行分配。

Returns
int the current capacity

charAt

Added in API level 1
char charAt (int index)

在指定索引处返回此序列中的char值。 第一个char值为索引0 ,下一个索引为1 ,依此类推,如同数组索引。

索引参数必须大于或等于 0 ,并且小于此序列的长度。

如果索引指定的 char值为 surrogate ,则返回代理值。

Parameters
index int: the index of the desired char value.
Returns
char the char value at the specified index.
Throws
IndexOutOfBoundsException if index is negative or greater than or equal to length().

codePointAt

Added in API level 1
int codePointAt (int index)

返回指定索引处的字符(Unicode码点)。 该索引指char值(Unicode代码单元),范围从0length() - 1

如果给定索引处指定的char值处于高代理范围内,则以下索引小于此序列的长度,并且以下索引处的char值处于低代理范围内,则补充代码点对应于这个代理对被返回。 否则,返回给定索引处的char值。

Parameters
index int: the index to the char values
Returns
int the code point value of the character at the index
Throws
IndexOutOfBoundsException if the index argument is negative or not less than the length of this sequence.

codePointBefore

Added in API level 1
int codePointBefore (int index)

返回指定索引之前的字符(Unicode码点)。 该索引是指char值(Unicode代码单元),范围从1length()

如果char在值(index - 1)处于低代理项范围, (index - 2)不为负,并且char在值(index - 2)处于高代理项范围,则返回代理对的补充代码点值。 如果char值为index - 1是未配对的低代理或高代理,则返回替代值。

Parameters
index int: the index following the code point that should be returned
Returns
int the Unicode code point value before the given index.
Throws
IndexOutOfBoundsException if the index argument is less than 1 or greater than the length of this sequence.

codePointCount

Added in API level 1
int codePointCount (int beginIndex, 
                int endIndex)

返回此序列的指定文本范围内的Unicode代码点数。 文本范围始于指定beginIndex并延伸到char在索引endIndex - 1 因此,文本范围的长度( char s)为endIndex-beginIndex 这个序列中的不配对代理每个都计为一个代码点。

Parameters
beginIndex int: the index to the first char of the text range.
endIndex int: the index after the last char of the text range.
Returns
int the number of Unicode code points in the specified text range
Throws
IndexOutOfBoundsException if the beginIndex is negative, or endIndex is larger than the length of this sequence, or beginIndex is larger than endIndex.

delete

Added in API level 1
StringBuilder delete (int start, 
                int end)

删除此序列的子字符串中的字符。 子字符串从指定的start开始,并且延伸到索引为end - 1的字符或者如果不存在这样的字符则延伸到序列的末尾。 如果start等于end ,则不做任何更改。

Parameters
start int: The beginning index, inclusive.
end int: The ending index, exclusive.
Returns
StringBuilder This object.
Throws
StringIndexOutOfBoundsException

deleteCharAt

Added in API level 1
StringBuilder deleteCharAt (int index)

按照此顺序移除指定位置的char 这个序列缩短了一个char

注意:如果给定索引处的字符是补充字符,则此方法不会删除整个字符。 如果需要的准确处理增补字符,确定数量char通过调用删除Character.charCount(thisSequence.codePointAt(index)) ,其中thisSequence是此序列。

Parameters
index int: Index of char to remove
Returns
StringBuilder This object.
Throws
StringIndexOutOfBoundsException

ensureCapacity

Added in API level 1
void ensureCapacity (int minimumCapacity)

确保容量至少等于规定的最小值。 如果当前容量小于参数,则会为新的内部阵列分配更大的容量。 新的容量是以下数据中的较大者:

  • The minimumCapacity argument.
  • Twice the old capacity, plus 2.
If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

Parameters
minimumCapacity int: the minimum desired capacity.

getChars

Added in API level 1
void getChars (int srcBegin, 
                int srcEnd, 
                char[] dst, 
                int dstBegin)

将字符从该序列复制到目标字符数组dst 要复制的第一个字符位于索引srcBegin ; 要复制的最后一个字符位于索引srcEnd-1 要复制的字符总数为srcEnd-srcBegin 字符被复制到dst的子dst从索引dstBegin开始到索引:

 dstbegin + (srcEnd-srcBegin) - 1
 

Parameters
srcBegin int: start copying at this offset.
srcEnd int: stop copying at this offset.
dst char: the array to copy the data into.
dstBegin int: offset into dst.
Throws
NullPointerException if dst is null.
IndexOutOfBoundsException if any of the following is true:
  • srcBegin is negative
  • dstBegin is negative
  • the srcBegin argument is greater than the srcEnd argument.
  • srcEnd is greater than this.length().
  • dstBegin+srcEnd-srcBegin is greater than dst.length

indexOf

Added in API level 1
int indexOf (String str)

返回指定子字符串第一次出现的此字符串中的索引。 返回的整数是最小的值k ,使得:

 this.toString().startsWith(str, k)
 
is true.

Parameters
str String: any string.
Returns
int if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
Throws
NullPointerException

indexOf

Added in API level 1
int indexOf (String str, 
                int fromIndex)

从指定索引处开始,返回指定子字符串第一次出现的此字符串中的索引。 返回的整数是最小值k ,其中:

     k >= Math.min(fromIndex, str.length()) &&
                   this.toString().startsWith(str, k)
 
If no such value of k exists, then -1 is returned.

Parameters
str String: the substring for which to search.
fromIndex int: the index from which to start the search.
Returns
int the index within this string of the first occurrence of the specified substring, starting at the specified index.
Throws
NullPointerException

insert

Added in API level 1
StringBuilder insert (int offset, 
                char[] str)

char数组参数的字符串表示形式插入到该序列中。

数组参数的字符被插入到该序列的内容中,位置由offset指示。 这个序列的长度增加了参数的长度。

总体效果就好像第二个参数通过方法 valueOf(char[])转换为字符串,然后该字符串的字符在指定的偏移量处插入此字符序列中的 ERROR(inserted/#insert(int,String) inserted)

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
str char: a character array.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                float f)

将参数 float的字符串表示插入到此序列中。

总体效果就好像第二个参数通过方法 valueOf(float)转换为字符串,然后该字符串的字符在指定的偏移量处 ERROR(inserted/#insert(int,String) inserted)到该字符序列中的 ERROR(inserted/#insert(int,String) inserted)

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
f float: a float.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int dstOffset, 
                CharSequence s)

将指定的 CharSequence插入到该序列中。

参数 CharSequence的字符按顺序插入指定偏移量的该序列中,向上移动位于该位置上方的任何字符,并将该序列的长度增加参数s的长度。

该方法的结果与调用此对象的方法 ERROR(insert/#insert(int,CharSequence,int,int) insert) (dstOffset,s,0,s.length()) ERROR(insert/#insert(int,CharSequence,int,int) insert)

如果 snull ,则将四个字符 "null"插入此序列中。

Parameters
dstOffset int: the offset.
s CharSequence: the sequence to be inserted
Returns
StringBuilder a reference to this object.
Throws
IndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                char c)

char参数的字符串表示插入到该序列中。

总体效果就好像第二个参数通过方法 valueOf(char)转换为字符串,然后该字符串中的字符以 ERROR(inserted/#insert(int,String) inserted)转换为指定偏移量处的该字符序列。

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
c char: a char.
Returns
StringBuilder a reference to this object.
Throws
IndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                long l)

long参数的字符串表示形式插入到该序列中。

总体效果就好像第二个参数通过方法 valueOf(long)转换为字符串,然后该字符串的字符在指定的偏移量处插入此字符序列中的 ERROR(inserted/#insert(int,String) inserted)

参数 offset必须大于或等于 0 ,并小于或等于此序列的长度。

Parameters
offset int: the offset.
l long: a long.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int index, 
                char[] str, 
                int offset, 
                int len)

str数组参数的子数组的字符串表示插入到该序列中。 该子阵列从指定的offset开始并延伸len char s。 index字符在index指示的位置插入到该序列中。 这个序列的长度增加len char s。

Parameters
index int: position at which to insert subarray.
str char: A char array.
offset int: the index of the first char in subarray to be inserted.
len int: the number of chars in the subarray to be inserted.
Returns
StringBuilder This object
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                int i)

将第二 int参数 int的字符串表示插入到该序列中。

总体效果就好像第二个参数通过方法 valueOf(int)转换为字符串,然后该字符串的字符随后在指定的偏移量处插入此字符序列中的 ERROR(inserted/#insert(int,String) inserted)

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
i int: an int.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                String str)

将字符串插入到此字符序列中。

参数String的字符按顺序插入指定偏移量的该序列中,向上移动原始位置上方的任何字符,并将该序列的长度增加参数的长度。 如果strnull ,则将四个字符"null"插入到该序列中。

新字符序列中索引 k处的字符等于:

  • the character at index k in the old character sequence, if k is less than offset
  • the character at index k-offset in the argument str, if k is not less than offset but is less than offset+str.length()
  • the character at index k-str.length() in the old character sequence, if k is not less than offset+str.length()

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
str String: a string.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                double d)

double参数的字符串表示插入到该序列中。

总体效果就好像第二个参数通过方法 valueOf(double)转换为字符串,然后该字符串的字符在指定的偏移量处为 ERROR(inserted/#insert(int,String) inserted)插入此字符序列中。

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
d double: a double.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int dstOffset, 
                CharSequence s, 
                int start, 
                int end)

将指定的 CharSequence序列插入到该序列中。

参数的子序列s通过指定startend被插入,以便成在指定的目的地偏移该序列中,该位置上述最初向上运动的任何字符。 这个序列的长度增加了end - start

在这个序列中的索引 k处的字符变成等于:

  • the character at index k in this sequence, if k is less than dstOffset
  • the character at index k+start-dstOffset in the argument s, if k is greater than or equal to dstOffset but is less than dstOffset+end-start
  • the character at index k-(end-start) in this sequence, if k is greater than or equal to dstOffset+end-start

参数 dstOffset必须大于或等于 0 ,并且小于或等于此序列的长度。

启动参数必须是非负的,并且不能大于 end

结束参数必须大于或等于 start ,并且小于或等于s的长度。

如果 snull ,则此方法插入字符,就好像s参数是包含四个字符 "null"的序列 "null"

Parameters
dstOffset int: the offset in this sequence.
s CharSequence: the sequence to be inserted.
start int: the starting index of the subsequence to be inserted.
end int: the end index of the subsequence to be inserted.
Returns
StringBuilder a reference to this object.
Throws
IndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                Object obj)

Object参数的字符串表示形式插入到该字符序列中。

总体效果就好像第二个参数通过方法 valueOf(Object)转换为字符串,然后该字符串的字符在指定的偏移量处插入此字符序列中的 ERROR(inserted/#insert(int,String) inserted)

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
obj Object: an Object.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

insert

Added in API level 1
StringBuilder insert (int offset, 
                boolean b)

将参数 boolean的字符串表示插入到该序列中。

整体效果就好像第二个参数通过方法 valueOf(boolean)转换为字符串,然后该字符串的字符在指定的偏移量处插入此字符序列中的 ERROR(inserted/#insert(int,String) inserted)

参数 offset必须大于或等于 0 ,并且小于或等于此序列的长度。

Parameters
offset int: the offset.
b boolean: a boolean.
Returns
StringBuilder a reference to this object.
Throws
StringIndexOutOfBoundsException

lastIndexOf

Added in API level 1
int lastIndexOf (String str, 
                int fromIndex)

返回指定子字符串最后一次出现的此字符串中的索引。 返回的整数是最大值k ,使得:

     k <= Math.min(fromIndex, str.length()) &&
                   this.toString().startsWith(str, k)
 
If no such value of k exists, then -1 is returned.

Parameters
str String: the substring to search for.
fromIndex int: the index to start the search from.
Returns
int the index within this sequence of the last occurrence of the specified substring.
Throws
NullPointerException

lastIndexOf

Added in API level 1
int lastIndexOf (String str)

返回指定子字符串最右边出现的字符串中的索引。 最右边的空字符串“”被认为发生在索引值this.length() 返回的索引是这样的最大值k

 this.toString().startsWith(str, k)
 
is true.

Parameters
str String: the substring to search for.
Returns
int if the string argument occurs one or more times as a substring within this object, then the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.
Throws
NullPointerException

length

Added in API level 1
int length ()

返回长度(字符数)。

Returns
int the length of the sequence of characters currently represented by this object

offsetByCodePoints

Added in API level 1
int offsetByCodePoints (int index, 
                int codePointOffset)

返回此序列中的索引,该索引与给定的index codePointOffset代码点偏移。 indexcodePointOffset给出的文本范围内的未配对代理每个计为一个代码点。

Parameters
index int: the index to be offset
codePointOffset int: the offset in code points
Returns
int the index within this sequence
Throws
IndexOutOfBoundsException if index is negative or larger then the length of this sequence, or if codePointOffset is positive and the subsequence starting with index has fewer than codePointOffset code points, or if codePointOffset is negative and the subsequence before index has fewer than the absolute value of codePointOffset code points.

replace

Added in API level 1
StringBuilder replace (int start, 
                int end, 
                String str)

用指定的String的字符替换此序列的子字符串中的字符。 子字符串从指定的start开始,并且延伸到索引为end - 1的字符或者如果不存在这样的字符则延伸到序列的末尾。 首先删除子字符串中的字符,然后在String处插入指定的start (如果需要,此序列将被延长以适应指定的字符串。)

Parameters
start int: The beginning index, inclusive.
end int: The ending index, exclusive.
str String: String that will replace previous contents.
Returns
StringBuilder This object.
Throws
StringIndexOutOfBoundsException

reverse

Added in API level 1
StringBuilder reverse ()

导致该字符序列被序列的反向替换。 如果序列中包含任何代理对,则这些代码将被视为反向操作的单个字符。 因此,高低代理人的顺序永远不会颠倒过来。 假设n是在执行reverse方法之前该字符序列的字符长度(不是char值的长度)。 然后,新字符序列中索引k处的字符等于旧字符序列中索引nk-1处的字符。

请注意,逆向操作可能会导致产生替代对,这些替代对在手术前是不配对的低代孕者和高代孕者。 例如,反转“\ uDC00 \ uD800”会生成“\ uD800 \ uDC00”,这是一个有效的代理对。

Returns
StringBuilder a reference to this object.

setCharAt

Added in API level 1
void setCharAt (int index, 
                char ch)

指定索引处的字符设置为ch 该序列被改变以表示一个新的字符序列,其是相同的原字符序列,不同之处在于它包含字符ch在位置index

索引参数必须大于或等于 0 ,并且小于此序列的长度。

Parameters
index int: the index of the character to modify.
ch char: the new character.
Throws
IndexOutOfBoundsException if index is negative or greater than or equal to length().

setLength

Added in API level 1
void setLength (int newLength)

设置字符序列的长度。 序列更改为新的字符序列,其长度由参数指定。 对于小于每非负索引k newLength ,在新的字符序列的索引k处的字符是相同的在旧序列索引k如果k小于原字符序列的长度的字符; 否则,它是空字符'\u0000' 换句话说,如果参数newLength小于当前长度,则长度将更改为指定的长度。

如果参数 newLength大于或等于当前长度,则会附加足够多的空字符( '\u0000' ),以使长度变为 newLength参数。

参数 newLength必须大于或等于 0

Parameters
newLength int: the new length
Throws
IndexOutOfBoundsException if the newLength argument is negative.

subSequence

Added in API level 1
CharSequence subSequence (int start, 
                int end)

返回一个新的字符序列,该序列是该序列的子序列。

这种形式的方法的调用

 sb.subSequence(begin, end)
behaves in exactly the same way as the invocation
 sb.substring(begin, end)
This method is provided so that this class can implement the CharSequence interface.

Parameters
start int: the start index, inclusive.
end int: the end index, exclusive.
Returns
CharSequence the specified subsequence.
Throws
IndexOutOfBoundsException if start or end are negative, if end is greater than length(), or if start is greater than end

substring

Added in API level 1
String substring (int start, 
                int end)

返回一个新的String ,其中包含当前包含在此序列中的字符的子序列。 子字符串从指定的start开始,并扩展到索引号为end - 1的字符。

Parameters
start int: The beginning index, inclusive.
end int: The ending index, exclusive.
Returns
String The new string.
Throws
StringIndexOutOfBoundsException if start or end are negative or greater than length(), or start is greater than end.

substring

Added in API level 1
String substring (int start)

返回一个新的String ,其中包含当前包含在此字符序列中的字符的子序列。 子字符串从指定的索引处开始并延伸到该序列的末尾。

Parameters
start int: The beginning index, inclusive.
Returns
String The new string.
Throws
StringIndexOutOfBoundsException if start is less than zero, or greater than the length of this object.

toString

Added in API level 1
String toString ()

返回表示此序列中数据的字符串。 一个新的String对象被分配并初始化以包含当前由该对象表示的字符序列。 String然后返回。 此序列的后续更改不会影响String的内容。

Returns
String a string representation of this sequence of characters.

trimToSize

Added in API level 1
void trimToSize ()

尝试减少用于字符序列的存储空间。 如果缓冲区比保留当前字符序列所需的缓冲区大,那么可能会调整缓冲区的大小以提高空间利用率。 调用此方法可能会影响随后调用capacity()方法返回的值,但不是必需的。

Hooray!