public class StringBuilderEx
extends java.lang.Object
implements java.io.Serializable, java.lang.Appendable, java.lang.CharSequence
StringBuilder
class, while adding a few additional methods.Modifier and Type | Field and Description |
---|---|
private java.lang.StringBuilder |
_sb |
(package private) static long |
serialVersionUID |
Constructor and Description |
---|
StringBuilderEx() |
StringBuilderEx(java.lang.CharSequence seq) |
StringBuilderEx(int capacity) |
StringBuilderEx(java.lang.String str) |
StringBuilderEx(java.lang.StringBuilder innerStringBuilder)
Use the given StringBuilder as the internal builder.
|
Modifier and Type | Method and Description |
---|---|
StringBuilderEx |
append(boolean b) |
StringBuilderEx |
append(char c) |
StringBuilderEx |
append(char[] str) |
StringBuilderEx |
append(char[] str,
int offset,
int len) |
StringBuilderEx |
append(java.lang.CharSequence s) |
StringBuilderEx |
append(java.lang.CharSequence s,
int start,
int end) |
StringBuilderEx |
append(double d) |
StringBuilderEx |
append(float f) |
StringBuilderEx |
append(int i) |
StringBuilderEx |
append(long lng) |
StringBuilderEx |
append(java.lang.Object obj) |
StringBuilderEx |
append(java.lang.String str) |
StringBuilderEx |
append(java.lang.StringBuffer sb)
Appends the specified
StringBuffer to this sequence. |
StringBuilderEx |
appendCodePoint(int codePoint) |
StringBuilderEx |
appendIfEmpty(java.lang.String str) |
StringBuilderEx |
appendIfNotEmpty(java.lang.String str) |
StringBuilderEx |
appendSeparator(java.lang.String separator)
Appends the given separator, but only if there is existing text and
it does not already end with the separator.
|
int |
capacity()
Returns the current capacity.
|
char |
charAt(int index)
Returns the
char value in this sequence at the specified index. |
java.util.stream.IntStream |
chars()
Returns a stream of
int zero-extending the char values
from this sequence. |
int |
codePointAt(int index)
Returns the character (Unicode code point) at the specified
index.
|
int |
codePointBefore(int index)
Returns the character (Unicode code point) before the specified
index.
|
int |
codePointCount(int beginIndex,
int endIndex)
Returns the number of Unicode code points in the specified text
range of this sequence.
|
java.util.stream.IntStream |
codePoints()
Returns a stream of code point values from this sequence.
|
StringBuilderEx |
delete(int start,
int end) |
StringBuilderEx |
deleteCharAt(int index) |
boolean |
endsWith(java.lang.String test) |
boolean |
endsWith(java.lang.String test,
boolean ignoreCase) |
void |
ensureCapacity(int minimumCapacity)
Ensures that the capacity is at least equal to the specified minimum.
|
void |
getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
Characters are copied from this sequence into the
destination character array
dst . |
java.lang.StringBuilder |
getStringBuilder()
Get the internal StringBuilder.
|
int |
indexOf(java.lang.String str) |
int |
indexOf(java.lang.String str,
int fromIndex) |
StringBuilderEx |
insert(int offset,
boolean b) |
StringBuilderEx |
insert(int offset,
char c) |
StringBuilderEx |
insert(int offset,
char[] str) |
StringBuilderEx |
insert(int index,
char[] str,
int offset,
int len) |
StringBuilderEx |
insert(int dstOffset,
java.lang.CharSequence s) |
StringBuilderEx |
insert(int dstOffset,
java.lang.CharSequence s,
int start,
int end) |
StringBuilderEx |
insert(int offset,
double d) |
StringBuilderEx |
insert(int offset,
float f) |
StringBuilderEx |
insert(int offset,
int i) |
StringBuilderEx |
insert(int offset,
long l) |
StringBuilderEx |
insert(int offset,
java.lang.Object obj) |
StringBuilderEx |
insert(int offset,
java.lang.String str) |
int |
lastIndexOf(java.lang.String str) |
int |
lastIndexOf(java.lang.String str,
int fromIndex) |
int |
length()
Returns the length (character count).
|
int |
offsetByCodePoints(int index,
int codePointOffset)
Returns the index within this sequence that is offset from the
given
index by codePointOffset code
points. |
StringBuilderEx |
replace(int start,
int end,
java.lang.String str) |
StringBuilderEx |
reverse() |
void |
setCharAt(int index,
char ch)
The character at the specified index is set to
ch . |
void |
setLength(int newLength)
Sets the length of the character sequence.
|
boolean |
startsWith(java.lang.String test) |
boolean |
startsWith(java.lang.String test,
boolean ignoreCase) |
java.lang.CharSequence |
subSequence(int start,
int end)
Returns a new character sequence that is a subsequence of this sequence.
|
java.lang.String |
substring(int start)
Returns a new
String that contains a subsequence of
characters currently contained in this character sequence. |
java.lang.String |
substring(int start,
int end)
Returns a new
String that contains a subsequence of
characters currently contained in this sequence. |
java.lang.String |
toString() |
StringBuilderEx |
trimEnd(char... remove)
Remove one or more instances of any of the specified characters from the end of the StringBuilder.
|
StringBuilderEx |
trimEnd(char remove)
Remove one or more instances of the specified character from the end of the StringBuilder.
|
StringBuilderEx |
trimEnd(char[] remove,
int limit)
Remove one or more instances of any of the specified characters from the end of the StringBuilder.
|
StringBuilderEx |
trimEnd(char remove,
int limit)
Remove one or more instances of the specified character from the end of the StringBuilder.
|
void |
trimToSize()
Attempts to reduce storage used for the character sequence.
|
StringBuilderEx |
truncate(int numChars)
Remove the specified number of characters from the end of the string.
|
private final java.lang.StringBuilder _sb
static final long serialVersionUID
public StringBuilderEx()
public StringBuilderEx(java.lang.CharSequence seq)
public StringBuilderEx(int capacity)
public StringBuilderEx(java.lang.String str)
public StringBuilderEx(java.lang.StringBuilder innerStringBuilder)
innerStringBuilder
- public StringBuilderEx append(boolean b)
public StringBuilderEx append(char c)
append
in interface java.lang.Appendable
public StringBuilderEx append(char[] str)
public StringBuilderEx append(char[] str, int offset, int len)
str
- offset
- len
- java.lang.IndexOutOfBoundsException
public StringBuilderEx append(java.lang.CharSequence s)
append
in interface java.lang.Appendable
public StringBuilderEx append(java.lang.CharSequence s, int start, int end)
append
in interface java.lang.Appendable
s
- start
- end
- java.lang.IndexOutOfBoundsException
public StringBuilderEx append(double d)
public StringBuilderEx append(float f)
public StringBuilderEx append(int i)
public StringBuilderEx append(long lng)
public StringBuilderEx append(java.lang.Object obj)
public StringBuilderEx append(java.lang.String str)
public StringBuilderEx append(java.lang.StringBuffer sb)
StringBuffer
to this sequence.
The characters of the StringBuffer
argument are appended,
in order, to this sequence, increasing the
length of this sequence by the length of the argument.
If _sb
is null
, then the four characters
"null"
are appended to this sequence.
Let n be the length of this character sequence just prior to
execution of the append
method. Then the character at index
k in the new character sequence is equal to the character at
index k in the old character sequence, if k is less than
n; otherwise, it is equal to the character at index k-n
in the argument _sb
.
sb
- the StringBuffer
to append.public StringBuilderEx appendCodePoint(int codePoint)
codePoint
- public StringBuilderEx appendIfEmpty(java.lang.String str)
public StringBuilderEx appendIfNotEmpty(java.lang.String str)
public StringBuilderEx appendSeparator(java.lang.String separator)
separator
- The separator string to append.StringBuilderEx
(to provide a fluent interface).public int capacity()
public char charAt(int index)
char
value in this sequence at the specified index.
The first char
value is at index 0
, the next at index
1
, and so on, as in array indexing.
The index argument must be greater than or equal to
0
, and less than the length of this sequence.
If the char
value specified by the index is a
surrogate, the surrogate
value is returned.
charAt
in interface java.lang.CharSequence
index
- the index of the desired char
value.char
value at the specified index.java.lang.IndexOutOfBoundsException
- if index
is
negative or greater than or equal to length()
.public java.util.stream.IntStream chars()
int
zero-extending the char
values
from this sequence. Any char which maps to a surrogate code
point is passed through uninterpreted.
If the sequence is mutated while the stream is being read, the result is undefined.
chars
in interface java.lang.CharSequence
public int codePointAt(int index)
char
values
(Unicode code units) and ranges from 0
to
length()
- 1
.
If the char
value specified at the given index
is in the high-surrogate range, the following index is less
than the length of this sequence, and the
char
value at the following index is in the
low-surrogate range, then the supplementary code point
corresponding to this surrogate pair is returned. Otherwise,
the char
value at the given index is returned.
index
- the index to the char
valuesindex
java.lang.IndexOutOfBoundsException
- if the index
argument is negative or not less than the length of this
sequence.public int codePointBefore(int index)
char
values
(Unicode code units) and ranges from 1
to length()
.
If the char
value at (index - 1)
is in the low-surrogate range, (index - 2)
is not
negative, and the char
value at (index -
2)
is in the high-surrogate range, then the
supplementary code point value of the surrogate pair is
returned. If the char
value at index -
1
is an unpaired low-surrogate or a high-surrogate, the
surrogate value is returned.
index
- the index following the code point that should be returnedjava.lang.IndexOutOfBoundsException
- if the index
argument is less than 1 or greater than the length
of this sequence.public int codePointCount(int beginIndex, int endIndex)
beginIndex
and extends to the char
at
index endIndex - 1
. Thus the length (in
char
s) of the text range is
endIndex-beginIndex
. Unpaired surrogates within
this sequence count as one code point each.beginIndex
- the index to the first char
of
the text range.endIndex
- the index after the last char
of
the text range.java.lang.IndexOutOfBoundsException
- if the
beginIndex
is negative, or endIndex
is larger than the length of this sequence, or
beginIndex
is larger than endIndex
.public java.util.stream.IntStream codePoints()
int
values which are then passed to the stream.
If the sequence is mutated while the stream is being read, the result is undefined.
codePoints
in interface java.lang.CharSequence
public StringBuilderEx delete(int start, int end)
start
- end
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx deleteCharAt(int index)
index
- java.lang.StringIndexOutOfBoundsException
public boolean endsWith(java.lang.String test)
public boolean endsWith(java.lang.String test, boolean ignoreCase)
public void ensureCapacity(int minimumCapacity)
minimumCapacity
argument.
2
.
minimumCapacity
argument is nonpositive, this
method takes no action and simply returns.
Note that subsequent operations on this object can reduce the
actual capacity below that requested here.minimumCapacity
- the minimum desired capacity.public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
dst
. The first character to
be copied is at index srcBegin
; the last character to
be copied is at index srcEnd-1
. The total number of
characters to be copied is srcEnd-srcBegin
. The
characters are copied into the subarray of dst
starting
at index dstBegin
and ending at index:
dstbegin + (srcEnd-srcBegin) - 1
srcBegin
- start copying at this offset.srcEnd
- stop copying at this offset.dst
- the array to copy the data into.dstBegin
- offset into dst
.java.lang.IndexOutOfBoundsException
- if any of the following is true:
srcBegin
is negative
dstBegin
is negative
srcBegin
argument is greater than
the srcEnd
argument.
srcEnd
is greater than
this.length()
.
dstBegin+srcEnd-srcBegin
is greater than
dst.length
public java.lang.StringBuilder getStringBuilder()
StringBuilderEx
does not store any state data
that would be altered by normal StringBuilder use.public int indexOf(java.lang.String str)
public int indexOf(java.lang.String str, int fromIndex)
public StringBuilderEx insert(int offset, boolean b)
offset
- b
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int offset, char c)
offset
- c
- java.lang.IndexOutOfBoundsException
public StringBuilderEx insert(int offset, char[] str)
offset
- str
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int index, char[] str, int offset, int len)
index
- str
- offset
- len
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int dstOffset, java.lang.CharSequence s)
dstOffset
- s
- java.lang.IndexOutOfBoundsException
public StringBuilderEx insert(int dstOffset, java.lang.CharSequence s, int start, int end)
dstOffset
- s
- start
- end
- java.lang.IndexOutOfBoundsException
public StringBuilderEx insert(int offset, double d)
offset
- d
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int offset, float f)
offset
- f
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int offset, int i)
offset
- i
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int offset, long l)
offset
- l
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int offset, java.lang.Object obj)
offset
- obj
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx insert(int offset, java.lang.String str)
offset
- str
- java.lang.StringIndexOutOfBoundsException
public int lastIndexOf(java.lang.String str)
public int lastIndexOf(java.lang.String str, int fromIndex)
public int length()
length
in interface java.lang.CharSequence
public int offsetByCodePoints(int index, int codePointOffset)
index
by codePointOffset
code
points. Unpaired surrogates within the text range given by
index
and codePointOffset
count as
one code point each.index
- the index to be offsetcodePointOffset
- the offset in code pointsjava.lang.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.public StringBuilderEx replace(int start, int end, java.lang.String str)
start
- end
- str
- java.lang.StringIndexOutOfBoundsException
public StringBuilderEx reverse()
public void setCharAt(int index, char ch)
ch
. This
sequence is altered to represent a new character sequence that is
identical to the old character sequence, except that it contains the
character ch
at position index
.
The index argument must be greater than or equal to
0
, and less than the length of this sequence.
index
- the index of the character to modify.ch
- the new character.java.lang.IndexOutOfBoundsException
- if index
is
negative or greater than or equal to length()
.public void setLength(int newLength)
newLength
, the character at
index k in the new character sequence is the same as the
character at index k in the old sequence if k is less
than the length of the old character sequence; otherwise, it is the
null character '\u0000'
.
In other words, if the newLength
argument is less than
the current length, the length is changed to the specified length.
If the newLength
argument is greater than or equal
to the current length, sufficient null characters
('\u0000'
) are appended so that
length becomes the newLength
argument.
The newLength
argument must be greater than or equal
to 0
.
newLength
- the new lengthjava.lang.IndexOutOfBoundsException
- if the
newLength
argument is negative.public boolean startsWith(java.lang.String test)
public boolean startsWith(java.lang.String test, boolean ignoreCase)
public java.lang.CharSequence subSequence(int start, int end)
An invocation of this method of the form
_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.subSequence
in interface java.lang.CharSequence
start
- the start index, inclusive.end
- the end index, exclusive.java.lang.IndexOutOfBoundsException
- if start
or end
are negative,
if end
is greater than length()
,
or if start
is greater than end
public java.lang.String substring(int start)
String
that 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.start
- The beginning index, inclusive.java.lang.StringIndexOutOfBoundsException
- if start
is
less than zero, or greater than the length of this object.public java.lang.String substring(int start, int end)
String
that contains a subsequence of
characters currently contained in this sequence. The
substring begins at the specified start
and
extends to the character at index end - 1
.start
- The beginning index, inclusive.end
- The ending index, exclusive.java.lang.StringIndexOutOfBoundsException
- if start
or end
are negative or greater than
length()
, or start
is
greater than end
.public java.lang.String toString()
toString
in interface java.lang.CharSequence
toString
in class java.lang.Object
public StringBuilderEx trimEnd(char... remove)
remove
- A list of characters to remove, if it any are found at the end of the StringBuilder.StringBuilderEx
(to provide a fluent interface).public StringBuilderEx trimEnd(char remove)
remove
- The character to remove, if it is found at the end of the StringBuilder.StringBuilderEx
(to provide a fluent interface).public StringBuilderEx trimEnd(char[] remove, int limit)
remove
- A list of characters to remove, if it any are found at the end of the StringBuilder.limit
- The maximum number of characters to remove from the end.
If this is 0, no trimming will be performed.
A value of -1 indicates no limit.StringBuilderEx
(to provide a fluent interface).public StringBuilderEx trimEnd(char remove, int limit)
remove
- The character to remove, if it is found at the end of the StringBuilder.limit
- The maximum number of characters to remove from the end.
If this is 0, no trimming will be performed.
A value of -1 indicates no limit.StringBuilderEx
(to provide a fluent interface).public void trimToSize()
capacity()
method.public StringBuilderEx truncate(int numChars)
numChars
- The number of characters to remove from the end.StringBuilderEx
(to provide a fluent interface).