- 所有已实现的接口:
-
Serializable,Appendable,CharSequence,Comparable<StringBuilder>
StringBuffer兼容的API,但不保证同步。该类设计用于作为StringBuffer的一个替代品,在字符串缓冲区被单个线程使用的情况下(通常是这种情况)。在大多数实现下,建议优先使用该类而不是StringBuffer,因为它在大多数情况下会更快。
StringBuilder的主要操作是append和insert方法,这两个方法被重载,可以接受任何类型的数据。每个方法都会将给定的数据转换为字符串,然后将该字符串的字符追加或插入到字符串构建器中。append方法总是将这些字符添加到构建器的末尾;insert方法在指定位置添加字符。
例如,如果z引用一个当前内容为"start"的字符串构建器对象,那么方法调用z.append("le")将使字符串构建器包含"startle",而z.insert(4, "le")将改变字符串构建器以包含"starlet"。
一般来说,如果sb是StringBuilder的一个实例,则sb.append(x)的效果与sb.insert(sb.length(), x)相同。
每个字符串构建器都有一个容量。只要字符串构建器中包含的字符序列的长度不超过容量,就不需要分配新的内部缓冲区。如果内部缓冲区溢出,它会自动变大。
StringBuilder的实例不适合多线程使用。如果需要同步,则建议使用StringBuffer。
除非另有说明,在该类的构造函数或方法中传递null参数将导致抛出NullPointerException。
- API注释:
-
StringBuilder实现了Comparable,但没有覆盖equals。因此,StringBuilder的自然排序与equals不一致。如果将StringBuilder对象用作SortedMap中的键或SortedSet中的元素,则应谨慎。有关更多信息,请参见Comparable,SortedMap或SortedSet。 - 自从:
- 1.5
- 参见:
-
Constructor Summary
ConstructorsConstructorDescription构造一个没有字符且初始容量为16个字符的字符串构建器。StringBuilder(int capacity) 构造一个没有字符且初始容量由capacity参数指定的字符串构建器。构造一个包含与指定CharSequence相同字符的字符串构建器。StringBuilder(String str) 构造一个初始化为指定字符串内容的字符串构建器。 -
Method Summary
Modifier and TypeMethodDescriptionappend(boolean b) 将boolean参数的字符串表示附加到序列。append(char c) 将char参数的字符串表示附加到此序列。append(char[] str) 将char数组参数的字符串表示附加到此序列。append(char[] str, int offset, int len) 将char数组参数的子数组的字符串表示附加到此序列。append(double d) 将double参数的字符串表示附加到此序列。append(float f) 将float参数的字符串表示附加到此序列。append(int i) 将int参数的字符串表示附加到此序列。append(long lng) 将long参数的字符串表示附加到此序列。将指定的字符序列附加到此Appendable。append(CharSequence s, int start, int end) 将指定CharSequence的子序列附加到此序列。将Object参数的字符串表示附加到此字符序列。将指定的字符串附加到此字符序列。append(StringBuffer sb) 将指定的StringBuffer附加到此序列。appendCodePoint(int codePoint) 将codePoint参数的字符串表示附加到此序列。intcapacity()返回当前容量。charcharAt(int index) 返回指定索引处此序列中的char值。chars()返回从此序列中的char值零扩展的int流。intcodePointAt(int index) 返回指定索引处的字符(Unicode代码点)。intcodePointBefore(int index) 返回指定索引之前的字符(Unicode代码点)。intcodePointCount(int beginIndex, int endIndex) 返回此序列的指定文本范围内的Unicode代码点数。返回从此序列中的代码点值的流。intcompareTo(StringBuilder another) 按字典顺序比较两个StringBuilder实例。delete(int start, int end) 删除此序列中子字符串中的字符。deleteCharAt(int index) 删除此序列中指定位置的char。voidensureCapacity(int minimumCapacity) 确保容量至少等于指定的最小值。voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此序列复制到目标字符数组dst中。int返回指定子字符串的第一次出现在此字符串中的索引。int返回指定子字符串的第一次出现在此字符串中的索引,从指定索引开始搜索。insert(int offset, boolean b) 将boolean参数的字符串表示插入到此序列中。insert(int offset, char c) 将char参数的字符串表示插入到此序列中。insert(int offset, char[] str) 将char数组参数的字符串表示插入到此序列中。insert(int index, char[] str, int offset, int len) 将str数组参数的子数组的字符串表示插入到此序列中。insert(int offset, double d) 将double参数的字符串表示插入到此序列中。insert(int offset, float f) 将float参数的字符串表示插入到此序列中。insert(int offset, int i) 将第二个int参数的字符串表示插入到此序列中。insert(int offset, long l) 将long参数的字符串表示插入到此序列中。insert(int dstOffset, CharSequence s) 将CharSequence参数插入到此序列中。insert(int dstOffset, CharSequence s, int start, int end) 将指定CharSequence的子序列插入到此序列中。将Object参数的字符串表示插入到此字符序列中。将字符串插入到此字符序列中。intlastIndexOf(String str) 返回指定子字符串的最后一次出现在此字符串中的索引。intlastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一次出现在此字符串中的索引,从指定索引开始向后搜索。intlength()返回长度(字符数)。intoffsetByCodePoints(int index, int codePointOffset) 返回给定index偏移codePointOffset代码点后的此序列中的索引。repeat(int codePoint, int count) 将count个codePoint参数的字符串表示重复附加到此序列。repeat(CharSequence cs, int count) 将count个指定的CharSequencecs的副本附加到此序列。用指定String中的字符替换此序列中子字符串中的字符。reverse()使此字符序列被其反向顺序替换。voidsetCharAt(int index, char ch) 指定索引处的字符设置为ch。voidsetLength(int newLength) 设置字符序列的长度。subSequence(int start, int end) 返回此序列的子序列的新字符序列。substring(int start) 返回一个包含当前字符序列中当前包含的字符子序列的新String。substring(int start, int end) 返回一个包含当前序列中当前包含的字符子序列的新String。toString()返回表示此序列中数据的字符串。void尝试减少用于字符序列的存储。Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods declared in interface java.lang.CharSequence
charAt, chars, codePoints, isEmpty, length, subSequence
-
Constructor Details
-
StringBuilder
public StringBuilder()构造一个没有字符且初始容量为16个字符的字符串构建器。 -
StringBuilder
public StringBuilder(int capacity) 构造一个没有字符且初始容量由capacity参数指定的字符串构建器。- 参数:
-
capacity- 初始容量。 - 抛出:
-
NegativeArraySizeException- 如果capacity参数小于0。
-
StringBuilder
构造一个初始化为指定字符串内容的字符串构建器。字符串构建器的初始容量为16加上字符串参数的长度。- 参数:
-
str- 缓冲区的初始内容。
-
StringBuilder
构造一个包含与指定CharSequence相同字符的字符串构建器。字符串构建器的初始容量为16加上CharSequence参数的长度。- 参数:
-
seq- 要复制的序列。
-
-
Method Details
-
compareTo
按字典顺序比较两个StringBuilder实例。此方法遵循与CharSequence.compare(this, another)方法中定义的字典比较规则相同。对于更精细的、区域敏感的字符串比较,请参考
Collator。- 指定者:
-
compareTo在接口Comparable<StringBuilder> - 参数:
-
another- 要比较的StringBuilder - 返回:
-
如果此
StringBuilder包含与参数StringBuilder相同的字符序列,则返回值为0;如果此StringBuilder在字典上小于StringBuilder参数,则返回负整数;如果此StringBuilder在字典上大于StringBuilder参数,则返回正整数。 - 自:
- 11
-
append
追加Object参数的字符串表示形式。整体效果就像参数通过方法
String.valueOf(Object)转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 参数:
-
obj- 一个Object。 - 返回:
- 对此对象的引用。
-
append
追加指定的字符串到此字符序列中。按顺序追加
String参数的字符,增加此序列的长度等于参数的长度。如果str为null,则追加四个字符"null"。设n为执行
append方法之前此字符序列的长度。然后新字符序列中索引k处的字符等于旧字符序列中索引k处的字符,如果k小于n;否则,它等于参数str中索引k-n处的字符。- 参数:
-
str- 一个字符串。 - 返回:
- 对此对象的引用。
-
append
追加指定的StringBuffer到此序列中。按顺序追加
StringBuffer参数的字符到此序列中,增加此序列的长度等于参数的长度。如果sb为null,则追加四个字符"null"到此序列中。设n为执行
append方法之前此字符序列的长度。然后新字符序列中索引k处的字符等于旧字符序列中索引k处的字符,如果k小于n;否则,它等于参数sb中索引k-n处的字符。- 参数:
-
sb- 要追加的StringBuffer。 - 返回:
- 对此对象的引用。
-
append
从接口复制的描述:Appendable追加指定的字符序列到此Appendable中。根据实现字符序列
csq的类,可能不会追加整个序列。例如,如果csq是一个CharBuffer,则要追加的子序列由缓冲区的位置和限制定义。- 指定者:
-
append在接口Appendable - 参数:
-
s- 要追加的字符序列。如果csq为null,则追加四个字符"null"到此Appendable中。 - 返回:
-
对此
Appendable的引用
-
append
追加指定CharSequence的子序列到此序列中。从参数
s的索引start开始的字符按顺序追加到此序列的内容中,直到(不包括)索引end。此序列的长度增加了end - start的值。设n为执行
append方法之前此字符序列的长度。然后此字符序列中索引k处的字符变为等于此序列中索引k处的字符,如果k小于n;否则,它等于参数s中索引k+start-n处的字符。如果
s为null,则此方法追加字符,就好像s参数是包含四个字符"null"的序列一样。- 指定者:
-
append在接口Appendable - 参数:
-
s- 要追加的序列。 -
start- 要追加的子序列的起始索引。 -
end- 要追加的子序列的结束索引。 - 返回:
- 对此对象的引用。
- 抛出:
-
IndexOutOfBoundsException- 如果start为负数,或start大于end或end大于s.length()
-
append
追加char数组参数的字符串表示形式到此序列中。按顺序追加数组参数的字符到此序列的内容中。此序列的长度增加了参数的长度。
整体效果就像参数通过方法
String.valueOf(char[])转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 参数:
-
str- 要追加的字符。 - 返回:
- 对此对象的引用。
-
append
追加char数组参数的子数组的字符串表示形式到此序列中。从索引
offset开始的char数组str的字符按顺序追加到此序列的内容中。此序列的长度增加了len的值。整体效果就像参数通过方法
String.valueOf(char[],int,int)转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 参数:
-
str- 要追加的字符。 -
offset- 要追加的第一个char的索引。 -
len- 要追加的char的数量。 - 返回:
- 对此对象的引用。
- 抛出:
-
IndexOutOfBoundsException- 如果offset < 0或len < 0或offset+len > str.length
-
append
追加boolean参数的字符串表示形式到此序列中。整体效果就像参数通过方法
String.valueOf(boolean)转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 参数:
-
b- 一个boolean。 - 返回:
- 对此对象的引用。
-
append
追加char参数的字符串表示形式到此序列中。参数追加到此序列的内容中。此序列的长度增加了
1。整体效果就像参数通过方法
String.valueOf(char)转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 指定者:
-
append在接口Appendable - 参数:
-
c- 一个char。 - 返回:
- 对此对象的引用。
-
append
追加int参数的字符串表示形式到此序列中。整体效果就像参数通过方法
String.valueOf(int)转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 参数:
-
i- 一个int。 - 返回:
- 对此对象的引用。
-
append
追加long参数的字符串表示形式到此序列中。整体效果就像参数通过方法
String.valueOf(long)转换为字符串,然后该字符串的字符被追加到此字符序列中一样。- 参数:
-
lng- 一个long。 - 返回:
- 对此对象的引用。
-
append
将float参数的字符串表示附加到此序列。总体效果就好像参数通过方法
String.valueOf(float)转换为字符串,然后该字符串的字符被附加到此字符序列中。- 参数:
-
f- 一个float。 - 返回:
- 对此对象的引用。
-
append
将double参数的字符串表示附加到此序列。总体效果就好像参数通过方法
String.valueOf(double)转换为字符串,然后该字符串的字符被附加到此字符序列中。- 参数:
-
d- 一个double。 - 返回:
- 对此对象的引用。
-
appendCodePoint
将codePoint参数的字符串表示附加到此序列。参数被附加到此序列的内容中。此序列的长度增加了
Character.charCount(codePoint)。总体效果就好像参数通过方法
Character.toChars(int)转换为char数组,然后该数组中的字符被附加到此字符序列中。- 参数:
-
codePoint- 一个Unicode代码点 - 返回:
- 对此对象的引用。
- 自版本:
- 1.5
-
delete
删除此序列的子字符串中的字符。子字符串从指定的start开始,直到索引end - 1处的字符,或者如果不存在这样的字符,则直到序列的末尾。如果start等于end,则不进行任何更改。- 参数:
-
start- 起始索引(包括)。 -
end- 结束索引(不包括)。 - 返回:
- 此对象。
- 抛出:
-
StringIndexOutOfBoundsException- 如果start为负数,大于length(),或大于end。
-
deleteCharAt
删除此序列中指定位置的char。此序列的长度减少了一个char。注意:如果给定索引处的字符是补充字符,则此方法不会删除整个字符。如果需要正确处理补充字符,请通过调用
Character.charCount(thisSequence.codePointAt(index))确定要删除的char数量,其中thisSequence是此序列。- 参数:
-
index- 要删除的char的索引 - 返回:
- 此对象。
- 抛出:
-
StringIndexOutOfBoundsException- 如果index为负数或大于或等于length()。
-
replace
用指定的String中的字符替换此序列的子字符串。子字符串从指定的start开始,直到索引end - 1处的字符,或者如果不存在这样的字符,则直到序列的末尾。首先删除子字符串中的字符,然后将指定的String插入到start处。(如果需要,此序列将延长以容纳指定的字符串。)- 参数:
-
start- 起始索引(包括)。 -
end- 结束索引(不包括)。 -
str- 将替换先前内容的字符串。 - 返回:
- 此对象。
- 抛出:
-
StringIndexOutOfBoundsException- 如果start为负数,大于length(),或大于end。
-
insert
将str数组参数的子数组的字符串表示插入到此序列中。子数组从指定的offset开始,延伸len个char。子数组的字符插入到此序列中由index指示的位置。此序列的长度增加了len个char。- 参数:
-
index- 要插入子数组的位置。 -
str- 一个char数组。 -
offset- 要插入的子数组中第一个char的索引。 -
len- 要插入的子数组中的char数量。 - 返回:
- 此对象
- 抛出:
-
StringIndexOutOfBoundsException- 如果index为负数或大于length(),或offset或len为负数,或(offset+len)大于str.length。
-
insert
将Object参数的字符串表示插入到此字符序列中。总体效果就好像第二个参数通过方法
String.valueOf(Object)转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,并且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
obj- 一个Object。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
insert
将字符串插入到此字符序列中。String参数的字符按顺序插入到此序列中指定的偏移量处,将原始位置在该位置上方的任何字符向上移动,并将此序列的长度增加指定参数的长度。如果str为null,则将四个字符"null"插入到此序列中。新字符序列中索引k处的字符等于:
- 旧字符序列中索引k处的字符,如果k小于
offset - 参数
str中索引k-offset处的字符,如果k不小于offset但小于offset+str.length() - 旧字符序列中索引k
-str.length()处的字符,如果k不小于offset+str.length()
offset参数必须大于或等于0,并且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
str- 一个字符串。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
- 旧字符序列中索引k处的字符,如果k小于
-
insert
将char数组参数的字符串表示插入到此序列中。数组参数的字符插入到此序列的内容中由
offset指示的位置。此序列的长度增加了参数的长度。总体效果就好像第二个参数通过方法
String.valueOf(char[])转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,并且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
str- 一个字符数组。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
insert
将指定的CharSequence插入到此序列中。CharSequence参数的字符按顺序插入到此序列中指定的偏移量处,将原始位置在该位置上方的任何字符向上移动,并将此序列的长度增加参数的长度。此方法的结果与将此对象的
insert(dstOffset, s, 0, s.length())方法调用完全相同。如果
s为null,则将四个字符"null"插入到此序列中。- 参数:
-
dstOffset- 偏移量。 -
s- 要插入的序列 - 返回:
- 对此对象的引用。
- 抛出:
-
IndexOutOfBoundsException- 如果偏移量无效。
-
insert
插入指定CharSequence的子序列到此序列中。参数
s的子序列由start和end指定,按顺序插入到此序列的指定目标偏移量处,移动原本在该位置上方的任何字符。此序列的长度增加end - start。此序列中索引k处的字符变为:
- 如果k小于
dstOffset,则为此序列中索引k处的字符 - 如果k大于或等于
dstOffset但小于dstOffset+end-start,则为参数s中索引k+start-dstOffset处的字符 - 如果k大于或等于
dstOffset+end-start,则为此序列中索引k-(end-start)处的字符
dstOffset参数必须大于或等于0,且小于或等于此序列的长度。起始参数必须为非负数,并且不大于
end。结束参数必须大于或等于
start,且小于或等于s的长度。如果
s为null,则此方法插入字符,就好像s参数是包含四个字符"null"的序列。- 参数:
-
dstOffset- 此序列中的偏移量。 -
s- 要插入的序列。 -
start- 要插入的子序列的起始索引。 -
end- 要插入的子序列的结束索引。 - 返回:
- 对此对象的引用。
- 抛出:
-
IndexOutOfBoundsException- 如果dstOffset为负数或大于this.length(),或start或end为负数,或start大于end或end大于s.length()
- 如果k小于
-
insert
将boolean参数的字符串表示插入到此序列中。整体效果就好像第二个参数通过方法
String.valueOf(boolean)转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
b- 一个boolean值。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
insert
将char参数的字符串表示插入到此序列中。整体效果就好像第二个参数通过方法
String.valueOf(char)转换为字符串,然后该字符串中的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
c- 一个char值。 - 返回:
- 对此对象的引用。
- 抛出:
-
IndexOutOfBoundsException- 如果偏移量无效。
-
insert
将第二个int参数的字符串表示插入到此序列中。整体效果就好像第二个参数通过方法
String.valueOf(int)转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
i- 一个int值。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
insert
将long参数的字符串表示插入到此序列中。整体效果就好像第二个参数通过方法
String.valueOf(long)转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
l- 一个long值。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
insert
将float参数的字符串表示插入到此序列中。整体效果就好像第二个参数通过方法
String.valueOf(float)转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
f- 一个float值。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
insert
将double参数的字符串表示插入到此序列中。整体效果就好像第二个参数通过方法
String.valueOf(double)转换为字符串,然后该字符串的字符被插入到此字符序列中指定的偏移量处。offset参数必须大于或等于0,且小于或等于此序列的长度。- 参数:
-
offset- 偏移量。 -
d- 一个double值。 - 返回:
- 对此对象的引用。
- 抛出:
-
StringIndexOutOfBoundsException- 如果偏移量无效。
-
indexOf
返回指定子字符串在此字符串中第一次出现的索引。返回的索引是最小值
k,满足以下条件:
如果不存在这样的this.toString().startsWith(str, k)k值,则返回-1。- 参数:
-
str- 要搜索的子字符串。 - 返回:
-
指定子字符串第一次出现的索引,如果没有这样的出现则返回
-1。
-
indexOf
返回指定子字符串在此字符串中从指定索引开始第一次出现的索引。返回的索引是最小值
k,满足以下条件:
如果不存在这样的k >= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)k值,则返回-1。- 参数:
-
str- 要搜索的子字符串。 -
fromIndex- 开始搜索的索引。 - 返回:
-
指定子字符串从指定索引开始第一次出现的索引,如果没有这样的出现则返回
-1。
-
lastIndexOf
返回指定子字符串在此字符串中最后一次出现的索引。空字符串""的最后一次出现被认为发生在索引值this.length()。返回的索引是最大值
k,满足以下条件:
如果不存在这样的this.toString().startsWith(str, k)k值,则返回-1。- 参数:
-
str- 要搜索的子字符串。 - 返回:
-
指定子字符串最后一次出现的索引,如果没有这样的出现则返回
-1。
-
lastIndexOf
返回指定子字符串在此字符串中从指定索引开始向后搜索的最后一次出现的索引。返回的索引是最大值
k,满足以下条件:
如果不存在这样的k <= Math.min(fromIndex, this.length()) && this.toString().startsWith(str, k)k值,则返回-1。- 参数:
-
str- 要搜索的子字符串。 -
fromIndex- 开始向后搜索的索引。 - 返回:
-
指定子字符串从指定索引开始向后搜索的最后一次出现的索引,如果没有这样的出现则返回
-1。
-
reverse
将此字符序列替换为其反向序列。如果序列中包含代理对,则这些代理对在反向操作中被视为单个字符。因此,高低代理对的顺序永远不会被颠倒。设n为执行reverse方法之前此字符序列的字符长度(不是char值的长度)。那么新字符序列中索引k处的字符等于旧字符序列中索引n-k-1处的字符。请注意,反向操作可能导致产生代理对,这些代理对在操作之前是未配对的低代理和高代理。例如,反向"\uDC00\uD800"会产生"\uD800\uDC00",这是一个有效的代理对。
- 返回:
- 对此对象的引用。
-
repeat
将count个codePoint参数的字符串表示重复到此序列中。此序列的长度增加了
count倍的字符串表示长度。通常使用
char表达式表示代码点。例如:// 将10个星号插入缓冲区 sb.repeat('*', 10);- 参数:
-
codePoint- 要追加的代码点 -
count- 要复制的次数 - 返回:
- 对此对象的引用。
- 抛出:
-
IllegalArgumentException- 如果指定的codePoint不是有效的Unicode代码点,或者count为负。 - 自:
- 21
-
repeat
将指定的CharSequencecs的count个副本附加到此序列中。此序列的长度增加了
count倍的CharSequence长度。如果
cs为null,则将字符串"null"的四个字符重复到此序列中。- 参数:
-
cs- 一个CharSequence -
count- 要复制的次数 - 返回:
- 对此对象的引用。
- 抛出:
-
IllegalArgumentException- 如果count为负 - 自:
- 21
-
toString
返回表示此序列数据的字符串。将分配并初始化一个新的String对象,以包含当前由此对象表示的字符序列。然后返回此String。对此序列的后续更改不会影响String的内容。- 指定者:
-
toString在接口CharSequence - 返回:
- 此字符序列的字符串表示。
-
length
public int length()返回长度(字符计数)。- 指定者:
-
length在接口CharSequence - 返回:
- 当前由此对象表示的字符序列的长度
-
capacity
public int capacity()返回当前容量。容量是可以存储的字符数(包括已写入的字符),超过该容量将进行分配。- 返回:
- 当前容量
-
ensureCapacity
public void ensureCapacity(int minimumCapacity) 确保容量至少等于指定的最小容量。如果当前容量小于参数,则将分配具有更大容量的新内部数组。新容量是以下两者中较大的一个:- 最小容量参数。
- 旧容量的两倍加上
2。
minimumCapacity参数为非正数,则此方法不执行任何操作,只是返回。请注意,此对象上的后续操作可能会将实际容量降低到此处请求的容量以下。- 参数:
-
minimumCapacity- 所需的最小容量。
-
trimToSize
public void trimToSize()尝试减少用于字符序列的存储空间。如果缓冲区大于必要来容纳其当前字符序列的空间,则可以调整大小以提高空间效率。调用此方法可能会影响后续调用capacity()方法返回的值,但不是必需的。 -
setLength
public void setLength(int newLength) 设置字符序列的长度。将序列更改为由参数指定长度的新字符序列。对于小于newLength的每个非负索引k,新字符序列中索引k处的字符与旧序列中索引k处的字符相同(如果k小于旧字符序列的长度);否则,它是空字符'\u0000'。换句话说,如果newLength参数小于当前长度,则长度将更改为指定的长度。如果
newLength参数大于或等于当前长度,则将附加足够的空字符('\u0000')以使长度成为newLength参数。newLength参数必须大于或等于0。- 参数:
-
newLength- 新长度 - 抛出:
-
IndexOutOfBoundsException- 如果newLength参数为负。
-
charAt
public char charAt(int index) 返回此序列中指定索引处的char值。第一个char值位于索引0处,下一个位于索引1处,依此类推,就像数组索引一样。索引参数必须大于或等于
0,且小于此序列的长度。如果由索引指定的
char值是一个代理,则返回代理值。- 指定者:
-
charAt在接口CharSequence - 参数:
-
index- 所需char值的索引。 - 返回:
-
指定索引处的
char值。 - 抛出:
-
IndexOutOfBoundsException- 如果index为负或大于或等于length()。
-
codePointAt
public int codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。索引是指char值(Unicode代码单元),范围从0到CharSequence.length()- 1。如果给定索引处指定的
char值在高代理范围内,则以下一个索引小于此序列的长度,并且以下一个索引处的char值在低代理范围内,则返回对应于此代理对的补充代码点。否则,返回给定索引处的char值。- 参数:
-
index-char值的索引 - 返回:
-
在
index处字符的代码点值 - 抛出:
-
IndexOutOfBoundsException- 如果index参数为负或不小于此序列的长度。
-
codePointBefore
public int codePointBefore(int index) 返回指定索引之前的字符(Unicode代码点)。索引是指char值(Unicode代码单元),范围从1到CharSequence.length()。如果
(index - 1)处的char值在低代理范围内,(index - 2)不为负,并且(index - 2)处的char值在高代理范围内,则返回代理对的补充代码点值。如果index - 1处的char值是未配对的低代理或高代理,则返回代理值。- 参数:
-
index- 应返回的代码点之后的索引 - 返回:
- 给定索引之前的Unicode代码点值。
- 抛出:
-
IndexOutOfBoundsException- 如果index参数小于1或大于此序列的长度。
-
codePointCount
public int codePointCount(int beginIndex, int endIndex) 返回此序列的指定文本范围内的Unicode代码点数。文本范围从指定的beginIndex开始,延伸到索引endIndex - 1处的char。因此,文本范围的长度(以char为单位)为endIndex-beginIndex。此序列中的未配对代理对计为一个代码点。- 参数:
-
beginIndex- 文本范围的第一个char的索引。 -
endIndex- 文本范围的最后一个char之后的索引。 - 返回:
- 指定文本范围内的Unicode代码点数
- 抛出:
-
IndexOutOfBoundsException- 如果beginIndex为负,或endIndex大于此序列的长度,或beginIndex大于endIndex。
-
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset) 返回此序列中给定index和codePointOffset代码点偏移量的偏移量。文本范围内的未配对代理对计为一个代码点。- Parameters:
-
index- the index to be offset -
codePointOffset- the offset in code points - Returns:
- the index within this sequence
- Throws:
-
IndexOutOfBoundsException- ifindexis negative or larger than the length of this sequence, or ifcodePointOffsetis positive and the subsequence starting withindexhas fewer thancodePointOffsetcode points, or ifcodePointOffsetis negative and the subsequence beforeindexhas fewer than the absolute value ofcodePointOffsetcode points.
-
getChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Characters are copied from this sequence into the destination character arraydst. The first character to be copied is at indexsrcBegin; the last character to be copied is at indexsrcEnd-1. The total number of characters to be copied issrcEnd-srcBegin. The characters are copied into the subarray ofdststarting at indexdstBeginand ending at index:dstbegin + (srcEnd-srcBegin) - 1- Parameters:
-
srcBegin- start copying at this offset. -
srcEnd- stop copying at this offset. -
dst- the array to copy the data into. -
dstBegin- offset intodst. - Throws:
-
IndexOutOfBoundsException- if any of the following is true:srcBeginis negativedstBeginis negative- the
srcBeginargument is greater than thesrcEndargument. srcEndis greater thanthis.length().dstBegin+srcEnd-srcBeginis greater thandst.length
-
setCharAt
public void setCharAt(int index, char ch) The character at the specified index is set toch. This sequence is altered to represent a new character sequence that is identical to the old character sequence, except that it contains the characterchat positionindex.The index argument must be greater than or equal to
0, and less than the length of this sequence.- Parameters:
-
index- the index of the character to modify. -
ch- the new character. - Throws:
-
IndexOutOfBoundsException- ifindexis negative or greater than or equal tolength().
-
substring
Returns a newStringthat contains a subsequence of characters currently contained in this character sequence. The substring begins at the specified index and extends to the end of this sequence.- Parameters:
-
start- The beginning index, inclusive. - Returns:
- The new string.
- Throws:
-
StringIndexOutOfBoundsException- ifstartis less than zero, or greater than the length of this object.
-
subSequence
Returns a new character sequence that is a subsequence of this sequence.An invocation of this method of the form
behaves in exactly the same way as the invocationsb.subSequence(begin, end)
This method is provided so that this class can implement thesb.substring(begin, end)CharSequenceinterface.- Specified by:
-
subSequencein interfaceCharSequence - Parameters:
-
start- the start index, inclusive. -
end- the end index, exclusive. - Returns:
- the specified subsequence.
- Throws:
-
IndexOutOfBoundsException- ifstartorendare negative, ifendis greater thanlength(), or ifstartis greater thanend
-
substring
Returns a newStringthat contains a subsequence of characters currently contained in this sequence. The substring begins at the specifiedstartand extends to the character at indexend - 1.- Parameters:
-
start- The beginning index, inclusive. -
end- The ending index, exclusive. - Returns:
- The new string.
- Throws:
-
StringIndexOutOfBoundsException- ifstartorendare negative or greater thanlength(), orstartis greater thanend.
-
chars
Returns a stream ofintzero-extending thecharvalues from this sequence. Any char which maps to a surrogate code point is passed through uninterpreted.The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.
- Specified by:
-
charsin interfaceCharSequence - Returns:
- an IntStream of char values from this sequence
- Since:
- 9
-
codePoints
Returns a stream of code point values from this sequence. Any surrogate pairs encountered in the sequence are combined as if by Character.toCodePoint and the result is passed to the stream. Any other code units, including ordinary BMP characters, unpaired surrogates, and undefined code units, are zero-extended tointvalues which are then passed to the stream.The stream binds to this sequence when the terminal stream operation commences (specifically, for mutable sequences the spliterator for the stream is late-binding). If the sequence is modified during that operation then the result is undefined.
- Specified by:
-
codePointsin interfaceCharSequence - Returns:
- an IntStream of Unicode code points from this sequence
- Since:
- 9
-