Class MutableString
- All Implemented Interfaces:
Serializable
,Appendable
,CharSequence
,Cloneable
,Comparable<MutableString>
Motivation
The classical Java string classes, String
and StringBuffer
(or StringBuilder
), lie at the extreme of a spectrum (immutable and mutable).
However, large-scale text indexing requires some features that are not provided by these classes: in particular, the possibility of using a mutable string, once frozen, in the same optimized way of an immutable string.
In a typical scenario you are dividing text into words (so you use a mutable string to
accumulate characters). Once you've got your word, you would like to check whether this word is
in a dictionary without creating a new object. However, equality of string builders
is not defined on their content, and storing words after a conversion to
String
will not help either, as then you would need to convert the current mutable
string into an immutable one (thus creating a new object) before deciding whether you need to
store it.
This class tries to make the best of both worlds, and thus aims at being a Better Mousetrap™.
You can read more details about the design of MutableString
in Paolo Boldi and
Sebastiano Vigna, “Mutable strings in
Java: Design, implementation and lightweight text-search algorithms”, Sci. Comput.
Programming, 54(1):3-23, 2005.
Features
Mutable strings come in two flavours: compact and loose. A mutable string created by the empty constructor or the constructor specifying a capacity is loose. All other constructors create compact mutable strings. In most cases, you can completely forget whether your mutable strings are loose or compact and get good performance.- Mutable strings occupy little space— their only attributes are a backing character array and an integer;
- their methods try to be as efficient as possible:for instance, if some limitation on a
parameter is implied by limitation on array access, we do not check it explicitly, and Bloom
filters are used to speed up
multi-character substitutions
; - they let you access directly the backing array (at your own risk);
- they implement
CharSequence
, so, for instance, you can match or split a mutable string against a regular expression using the standard Java API; - they implement
Appendable
, so they can be used withFormatter
and similar classes; null
is not accepted as a string argument;- compact mutable strings have a slow growth; loose mutable strings have a fast growth;
- hash codes of compact mutable strings are cached (for faster equality checks);
- all search-based methods ({@link #indexOf(MutableString,int}, etc.) use a mini (single-word)
Bloom filter to implement the last-character heuristics from the Boyer–Moore
algorithm—they are much faster than the
String
counterparts; - typical conversions such as trimming, upper/lower casing and replacements are made in place, with minimal reallocations;
- all methods try, whenever it is possible, to return
this
, so you can chain methods as ins.length(0).append("foo").append("bar")
; - you can write or print a mutable string without creating a
String
by usingwrite(Writer)
,print(PrintWriter)
andprintln(PrintWriter)
; you can read it back usingread(Reader,int)
. - you can write any mutable string in (length-prefixed) UTF-8 format by using
writeSelfDelimUTF8(DataOutput)
—you are not limited to strings whose UTF-8 encoded length fits 16 bits; notice however that surrogate pairs will not be coalesced into a single code point, so you must re-read such strings using the same method; - you can
wrap
any character array into a mutable string; - this class is not final: thus, you can add your own methods to specialized versions.
Committing to use this class for such an ubiquitous data structure as strings may seem dangerous, as standard string classes are by now tested and stable. However, this class has been heavily regression (and torture) tested on all methods, and we believe it is very reliable.
To simplify the transition to mutable strings, we have tried to make mixing string classes
simpler by providing polymorphic versions of all methods accepting one or more
strings—whenever you must specify a string you can usually provide a
MutableString
, a String
, or a generic CharSequence
.
Note that usually we provide a specific method for String
. This duplication may seem
useless, as String
implements CharSequence
. However, invoking methods
on an interface is slower than invoking methods on a class, and we expect constant strings to
appear often in such methods.
The Reallocation Heuristic
Backing array reallocations use a heuristic based on looseness. Whenever an operation changes the length, compact strings are resized to fit exactly the new content, whereas the capacity of a loose string is never shortened, and enlargements maximize the new length required with the double of the current capacity.
The effect of this policy is that loose strings will get large buffers quickly, but compact strings will occupy little space and perform very well in data structures using hash codes.
For instance, you can easily reuse a loose mutable string calling length(0)
(which does not reallocate the backing array).
In any case, you can call compact()
and loose()
to force the respective
condition.
Disadvantages
The main disadvantage of mutable strings is that their substrings cannot share their backing
arrays, so if you need to generate many substrings you may want to use String
.
However, subSequence()
returns a CharSequence
that shares
the backing array.
Warnings
There are a few differences with standard string classes you should be aware of.- This class is not synchronized. If multiple threads access an object of this class concurrently, and at least one of the threads modifies it, it must be synchronized externally.
- This class implements polymorphic versions of the
equals
method that compare the content ofString
s andCharSequence
s, so that you can easily do checks likemutableString.equals("Hello")
Thus, you must not mix mutable strings withCharSequence
s in collections as equality between objects of those types is not symmetric. - When the length of a string or char array argument is zero, some methods may just do nothing even if other parameters are out of bounds.
- The output of
writeSelfDelimUTF8()
is not compatible with the usual JavawriteUTF()
. - Even if this class is not final, most methods are declared final for efficiency, so
you cannot override them (why should you ever want to override
array()
?).
- Since:
- 0.3
- Author:
- Sebastiano Vigna, Paolo Boldi
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected char[]
The backing array.protected int
This mutable string is compact iff this attribute is negative.static final long
-
Constructor Summary
ConstructorDescriptionCreates a new loose empty mutable string with capacity 2.MutableString
(char[] a) Creates a new compact mutable string copying a given character array.MutableString
(char[] a, int offset, int len) Creates a new compact mutable string copying a part of a given character array.MutableString
(int capacity) Creates a new loose empty mutable string with given capacity.Creates a new compact mutable string copying a given mutable string.Creates a new compact mutable string copying a givenCharSequence
.Creates a new compact mutable string copying a givenString
. -
Method Summary
Modifier and TypeMethodDescriptionfinal MutableString
append
(boolean b) Appends a boolean to this mutable string.final MutableString
append
(char c) Appends a character to this mutable string.final MutableString
append
(char[] a) Appends the given character array to this mutable string.final MutableString
append
(char[] a, int offset, int len) Appends a part of the given character array to this mutable string.final MutableString
append
(double d) Appends a double to this mutable string.final MutableString
append
(float f) Appends a float to this mutable string.final MutableString
append
(int i) Appends an integer to this mutable string.final MutableString
append
(long l) Appends a long to this mutable string.final MutableString
Appends the given character list to this mutable string.final MutableString
Appends a part of the given character list to this mutable string.final MutableString
Appends the given mutable string to this mutable string.final MutableString
Appends the givenCharSequence
to this mutable string.final MutableString
append
(CharSequence[] a, int offset, int length, CharSequence separator) Appends the given character sequences to this mutable string using the given separator.final MutableString
append
(CharSequence[] a, CharSequence separator) Appends the given character sequences to this mutable string using the given separator.final MutableString
append
(CharSequence s, int start, int end) Appends a subsequence of the givenCharSequence
to this mutable string.final MutableString
Appends the string representation of an object to this mutable string.final MutableString
append
(Object[] a, int offset, int length, CharSequence separator) Appends the string representations of the given objects to this mutable string using the given separator.final MutableString
append
(Object[] a, CharSequence separator) Appends the string representations of the given objects to this mutable string using the given separator.final MutableString
Appends the givenString
to this mutable string.final char[]
array()
Gets the backing array.final int
capacity()
Returns the current length of the backing array.final MutableString
changed()
Invalidates the current cached hash code if this mutable string is compact.final char
charAt
(int index) Gets a character.final MutableString
charAt
(int index, char c) Sets the character at the given index.clone()
Creates a new compact mutable string by copying this one.final MutableString
compact()
Makes this mutable string compact (see the class description).final int
Compares this mutable string to another mutable string performing a lexicographical comparison.final int
Compares this mutable string to a character sequence performing a lexicographical comparison.final int
Compares this mutable string to a string performing a lexicographical comparison.final int
Compares this mutable string to another object disregarding case.final int
Type-specific version ofcompareToIgnoreCase()
.final int
Type-specific version ofcompareToIgnoreCase()
.copy()
Creates a new compact mutable string by copying this one.int
cospan
(char[] c) Spans the initial segment of this mutable string made of the complement of the specified characters.int
cospan
(char[] c, int from) Spans a segment of this mutable string made of the complement of the specified characters.int
Spans the initial segment of this mutable string made of the complement of the specified characters.int
Spans a segment of this mutable string made of the complement of the specified characters.final MutableString
delete
(char c) Removes all occurrences of the given character.final MutableString
delete
(char[] c) Removes all occurrences of the given characters.final MutableString
delete
(int start, int end) Removes the characters of this mutable string with indices in the range fromstart
(inclusive) toend
(exclusive).final MutableString
Removes all occurrences of the given characters.final MutableString
deleteCharAt
(int index) Removes the character at the given index.final boolean
endsWith
(MutableString suffix) Returns whether this mutable string ends with the given mutable string.final boolean
endsWith
(CharSequence suffix) Returns whether this mutable string ends with the given character sequence.final boolean
Returns whether this mutable string ends with the given string.final boolean
endsWithIgnoreCase
(MutableString suffix) Returns whether this mutable string ends with the given mutable string disregarding case.final boolean
endsWithIgnoreCase
(CharSequence suffix) Returns whether this mutable string ends with the given character sequence disregarding case.final boolean
endsWithIgnoreCase
(String suffix) Returns whether this mutable string ends with the given string disregarding case.final MutableString
ensureCapacity
(int minimumCapacity) Ensures that at least the given number of characters can be stored in this mutable string.final boolean
Type-specific version ofequals()
.final boolean
Type-specific version ofequals()
.final boolean
Compares this mutable string to another object.final boolean
Type-specific version ofequals()
.final boolean
Checks two mutable strings for equality ignoring case.final boolean
Type-specific version ofequalsIgnoreCase()
.final boolean
Type-specific version ofequalsIgnoreCase()
.final char
Returns the first character of this mutable string.final void
getChars
(int start, int end, char[] dest, int destStart) Characters with indices fromstart
(inclusive) to indexend
(exclusive) are copied from this mutable string into the arraydest
, starting from indexdestStart
.static void
getChars
(CharSequence s, int start, int end, char[] dest, int destStart) Commodity static method implementingString.getChars(int,int,char[],int)
for aCharSequence
s.final int
hashCode()
Returns a hash code for this mutable string.final int
indexOf
(char c) Returns the index of the first occurrence of the specified character.final int
indexOf
(char c, int from) Returns the index of the first occurrence of the specified character, starting at the specified index.final int
indexOf
(MutableString pattern) Returns the index of the first occurrence of the specified mutable string.final int
indexOf
(MutableString pattern, int from) Returns the index of the first occurrence of the specified mutable string, starting at the specified index.int
indexOf
(TextPattern pattern) Returns the index of the first occurrence of the specified text pattern, starting at the specified index.int
indexOf
(TextPattern pattern, int from) Returns the index of the first occurrence of the specified text pattern, starting at the specified index.final int
indexOf
(CharSequence pattern) Returns the index of the first occurrence of the specified character sequence.final int
indexOf
(CharSequence pattern, int from) Returns the index of the first occurrence of the specified character sequence, starting at the specified index.int
indexOfAnyBut
(char[] c) Returns the index of the first occurrence of any character, except those specified.int
indexOfAnyBut
(char[] c, int from) Returns the index of the first occurrence of any character, except those specified, starting at the specified index.int
Returns the index of the first occurrence of any character, except those specified.int
indexOfAnyBut
(CharSet s, int from) Returns the index of the first occurrence of any character, except those specified, starting at the specified index.int
indexOfAnyOf
(char[] c) Returns the index of the first occurrence of any of the specified characters.int
indexOfAnyOf
(char[] c, int from) Returns the index of the first occurrence of any of the specified characters, starting at the specified index.int
Returns the index of the first occurrence of any of the specified characters.int
indexOfAnyOf
(CharSet s, int from) Returns the index of the first occurrence of any of the specified characters, starting at the specified index.final MutableString
insert
(int index, boolean b) Inserts a boolean in this mutable string, starting from indexindex
.final MutableString
insert
(int index, char c) Inserts a char in this mutable string, starting from indexindex
.final MutableString
insert
(int index, char[] c) Inserts characters in this mutable string.final MutableString
insert
(int index, char[] c, int offset, int len) Inserts characters in this mutable string.final MutableString
insert
(int index, double d) Inserts a double in this mutable string, starting from indexindex
.final MutableString
insert
(int index, float f) Inserts a float in this mutable string, starting from indexindex
.final MutableString
insert
(int index, int x) Inserts an int in this mutable string, starting from indexindex
.final MutableString
insert
(int index, long l) Inserts a long in this mutable string, starting from indexindex
.final MutableString
insert
(int index, MutableString s) Inserts a mutable string in this mutable string, starting from indexindex
.final MutableString
insert
(int index, CharSequence s) Inserts aCharSequence
in this mutable string, starting from indexindex
.final MutableString
Inserts the string representation of an object in this mutable string, starting from indexindex
.final MutableString
Inserts aString
in this mutable string, starting from indexindex
.final boolean
Returns whether this mutable string is compact (see the class description).final boolean
isEmpty()
Returns whether this mutable string is empty.final boolean
isLoose()
Returns whether this mutable string is loose (see the class description).final char
lastChar()
Returns the last character of this mutable string.final int
lastIndexOf
(char c) Returns the index of the last occurrence of the specified character.final int
lastIndexOf
(char c, int from) Returns the index of the last occurrence of the specified character, searching backward starting at the specified index.final int
lastIndexOf
(MutableString pattern) Returns the index of the last occurrence of the specified mutable string.final int
lastIndexOf
(MutableString pattern, int from) Returns the index of the last occurrence of the specified mutable string, searching backward starting at the specified index.final int
lastIndexOf
(CharSequence pattern) Returns the index of the last occurrence of the specified character sequence.final int
lastIndexOf
(CharSequence pattern, int from) Returns the index of the last occurrence of the specified character sequence, searching backward starting at the specified index.int
lastIndexOfAnyBut
(char[] c) Returns the index of the last occurrence of any character, except those specified.int
lastIndexOfAnyBut
(char[] c, int from) Returns the index of the last occurrence of any character, except those specified, starting at the specified index.int
Returns the index of the last occurrence of any character, except those specified.int
lastIndexOfAnyBut
(CharSet s, int from) Returns the index of the last occurrence of any character, except those specified, starting at the specified index.int
lastIndexOfAnyOf
(char[] c) Returns the index of the last occurrence of any of the specified characters.int
lastIndexOfAnyOf
(char[] c, int from) Returns the index of the last occurrence of any of the specified characters, searching backwards starting at the specified index.int
Returns the index of the last occurrence of any of the specified characters.int
lastIndexOfAnyOf
(CharSet s, int from) Returns the index of the last occurrence of any of the specified characters, searching backwards starting at the specified index.final int
length()
Returns the number of characters in this mutable string.final MutableString
length
(int newLength) Sets the length.final MutableString
loose()
Makes this mutable string loose.final void
print
(PrintStream s) Prints this mutable string to aPrintStream
.final void
print
(PrintWriter w) Prints this mutable string to aPrintWriter
.final void
Prints this mutable string to aPrintStream
and then terminates the line.final void
Prints this mutable string to aPrintWriter
and then terminates the line.final int
Reads a mutable string that has been written bywrite(Writer)
from aReader
.final MutableString
Reads a mutable string that has been written bywriteSelfDelimUTF8()
from aDataInput
.final MutableString
Reads a mutable string that has been written bywriteSelfDelimUTF8()
from anInputStream
.final MutableString
Deprecated.This method will not process surrogate pairs correctly.final MutableString
readUTF8
(InputStream s, int length) Deprecated.This method will not process surrogate pairs correctly.final MutableString
replace
(char c) Replaces the content of this mutable string with the given character.final MutableString
replace
(char[] c, char[] r) Replaces each occurrence of a set characters with a corresponding character.final MutableString
replace
(char[] c, MutableString[] s) Replaces each occurrence of a set of characters with a corresponding mutable string.final MutableString
replace
(char[] c, CharSequence[] s) Replaces each occurrence of a set of characters with a corresponding character sequence.final MutableString
Replaces each occurrence of a set of characters with a corresponding string.final MutableString
replace
(char c, char r) Replaces each occurrence of a character with a corresponding character.final MutableString
replace
(char c, MutableString s) Replaces each occurrence of a character with a corresponding mutable string.final MutableString
replace
(char c, CharSequence s) Replaces each occurrence of a character with a corresponding character sequence.final MutableString
Replaces each occurrence of a character with a corresponding string.final MutableString
replace
(int start, int end, char c) Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the given character.final MutableString
replace
(int start, int end, MutableString s) Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the given mutable string.final MutableString
replace
(int start, int end, CharSequence s) Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the givenCharSequence
.final MutableString
Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the givenString
.final MutableString
Replaces characters following a replacement map.final MutableString
Replaces the content of this mutable string with the given mutable string.final MutableString
Replaces each occurrence of a mutable string with a corresponding mutable string.final MutableString
Replaces the content of this mutable string with the given character sequence.final MutableString
replace
(CharSequence s, CharSequence r) Replaces each occurrence of a character sequence with a corresponding character sequence.final MutableString
Replaces the content of this mutable string with the given string.final MutableString
Replaces each occurrence of a string with a corresponding string.final MutableString
reverse()
The characters in this mutable string get reversed.final MutableString
setCharAt
(int index, char c) A nickname forcharAt(int,char)
.final MutableString
setLength
(int newLength) A nickname forlength(int)
.static int
Skips a string encoded bywriteSelfDelimUTF8(OutputStream)
.int
span
(char[] c) Spans the initial segment of this mutable string made of the specified characters.int
span
(char[] c, int from) Spans a segment of this mutable string made of the specified characters.int
Spans the initial segment of this mutable string made of the specified characters.int
Spans a segment of this mutable string made of the specified characters.final MutableString
Squeezes and normalizes spaces in this mutable string.final MutableString
squeezeSpaces
(boolean squeezeOnlyWhitespace) Squeezes and normalizes spaces in this mutable string.final MutableString
Squeezes and normalizes whitespace in this mutable string.final boolean
startsWith
(MutableString prefix) Returns whether this mutable string starts with the given mutable string.final boolean
startsWith
(CharSequence prefix) Returns whether this mutable string starts with the given character sequence.final boolean
startsWith
(String prefix) Returns whether this mutable string starts with the given string.final boolean
startsWithIgnoreCase
(MutableString prefix) Returns whether this mutable string starts with the given mutable string disregarding case.final boolean
startsWithIgnoreCase
(CharSequence prefix) Returns whether this mutable string starts with the given character sequence disregarding case.final boolean
startsWithIgnoreCase
(String prefix) Returns whether this mutable string starts with the given string disregarding case.final CharSequence
subSequence
(int start, int end) Returns a subsequence of this mutable string.final MutableString
substring
(int start) Returns a substring of this mutable string.final MutableString
substring
(int start, int end) Returns a substring of this mutable string.final char[]
Converts this string to a new character array.final MutableString
Converts all of the characters in this mutable string to lower case using the rules of the default locale.final String
toString()
final MutableString
Converts all of the characters in this mutable string to upper case using the rules of the default locale.final MutableString
trim()
Trims all leading and trailing whitespace from this string.final MutableString
trimLeft()
Trims all leading whitespace from this string.final MutableString
Trims all trailing whitespace from this string.static MutableString
wrap
(char[] a) Wraps a given character array in a compact mutable string.static MutableString
wrap
(char[] a, int length) Wraps a given character array for a given length in a loose mutable string.final void
Writes this mutable string to aWriter
.final void
Writes this mutable string to aDataOutput
as a length followed by a UTF-8 encoding.final void
Writes this mutable string to anOutputStream
as a length followed by a UTF-8 encoding.final void
Deprecated.This method will not process surrogate pairs correctly.final void
Deprecated.This method will not process surrogate pairs correctly.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.CharSequence
chars, codePoints
-
Field Details
-
array
protected transient char[] arrayThe backing array. -
hashLength
protected transient int hashLengthThis mutable string is compact iff this attribute is negative. It the string is compact, the attribute is its hash code (-1 denotes the invalid hash code). If the string is loose, the attribute is the number of characters actually stored in the backing array. -
serialVersionUID
public static final long serialVersionUID- See Also:
-
-
Constructor Details
-
MutableString
public MutableString()Creates a new loose empty mutable string with capacity 2. -
MutableString
public MutableString(int capacity) Creates a new loose empty mutable string with given capacity.- Parameters:
capacity
- the required capacity.
-
MutableString
Creates a new compact mutable string copying a given mutable string.- Parameters:
s
- the initial contents of the string.
-
MutableString
Creates a new compact mutable string copying a givenString
.- Parameters:
s
- the initial contents of the string.
-
MutableString
Creates a new compact mutable string copying a givenCharSequence
.- Parameters:
s
- the initial contents of the string.
-
MutableString
public MutableString(char[] a) Creates a new compact mutable string copying a given character array.- Parameters:
a
- the initial contents of the string.
-
MutableString
public MutableString(char[] a, int offset, int len) Creates a new compact mutable string copying a part of a given character array.- Parameters:
a
- a character array.offset
- an offset into the array.len
- how many characters to copy.
-
-
Method Details
-
copy
Creates a new compact mutable string by copying this one.- Returns:
- a compact copy of this mutable string.
-
clone
Creates a new compact mutable string by copying this one.This method is identical to
copy()
, but the latter returns a more specific type. -
getChars
Commodity static method implementingString.getChars(int,int,char[],int)
for aCharSequence
s.- Parameters:
s
- aCharSequence
.start
- copy start from this index (inclusive).end
- copy ends at this index (exclusive).dest
- destination array.destStart
- the first character will be copied indest[destStart]
.- See Also:
-
length
public final int length()Returns the number of characters in this mutable string.- Specified by:
length
in interfaceCharSequence
- Returns:
- the length of this mutable string.
-
isEmpty
public final boolean isEmpty()Returns whether this mutable string is empty.- Specified by:
isEmpty
in interfaceCharSequence
- Returns:
- whether this mutable string is empty.
-
capacity
public final int capacity()Returns the current length of the backing array.- Returns:
- the current length of the backing array.
-
array
public final char[] array()Gets the backing array.For fast, repeated access to the characters of this mutable string, you can obtain the actual backing array. Be careful, and if this mutable string is compact do not modify its backing array without calling
changed()
immediately afterwards (or the cached hash code will get out of sync, with unforeseeable consequences).- Returns:
- the backing array.
- See Also:
-
getChars
public final void getChars(int start, int end, char[] dest, int destStart) Characters with indices fromstart
(inclusive) to indexend
(exclusive) are copied from this mutable string into the arraydest
, starting from indexdestStart
.- Parameters:
start
- copy start from this index (inclusive).end
- copy ends at this index (exclusive).dest
- destination array.destStart
- the first character will be copied indest[destStart]
.- Throws:
NullPointerException
- ifdest
isnull
.IndexOutOfBoundsException
- if any of the following is true:start
orend
is negativestart
is greater thanend
end
is greater thanlength()
end-start+destStart
is greater thandest.length
- See Also:
-
ensureCapacity
Ensures that at least the given number of characters can be stored in this mutable string.The new capacity of this string will be exactly equal to the provided argument if this mutable string is compact (this differs markedly from
StringBuffer
). If this mutable string is loose, the provided argument is maximized with the current capacity doubled.Note that if the given argument is greater than the current length, you will make this string loose (see the class description).
- Parameters:
minimumCapacity
- we want at least this number of characters, but no more.- Returns:
- this mutable string.
-
length
Sets the length.If the provided length is greater than that of the current string, the string is padded with zeros. If it is shorter, the string is truncated to the given length. We do not reallocate the backing array, to increase object reuse. Use rather
compact()
for that purpose.Note that shortening a string will make it loose (see the class description).
- Parameters:
newLength
- the new length for this mutable string.- Returns:
- this mutable string.
- See Also:
-
setLength
A nickname forlength(int)
.- Parameters:
newLength
- the new length for this mutable string.- Returns:
- this mutable string.
- See Also:
-
compact
Makes this mutable string compact (see the class description).Note that this operation may require reallocating the backing array (of course, with a shorter length).
- Returns:
- this mutable string.
- See Also:
-
loose
Makes this mutable string loose.- Returns:
- this mutable string.
- See Also:
-
isCompact
public final boolean isCompact()Returns whether this mutable string is compact (see the class description).- Returns:
- whether this mutable string is compact.
- See Also:
-
isLoose
public final boolean isLoose()Returns whether this mutable string is loose (see the class description).- Returns:
- whether this mutable string is loose.
- See Also:
-
changed
Invalidates the current cached hash code if this mutable string is compact.You will need to call this method only if you change the backing array of a compact mutable string directly.
- Returns:
- this mutable string.
-
wrap
Wraps a given character array in a compact mutable string.The returned mutable string will be compact and backed by the given character array.
- Parameters:
a
- a character array.- Returns:
- a compact mutable string backed by the given array.
-
wrap
Wraps a given character array for a given length in a loose mutable string.The returned mutable string will be loose and backed by the given character array.
- Parameters:
a
- a character array.length
- a length.- Returns:
- a loose mutable string backed by the given array with the given length.
-
charAt
public final char charAt(int index) Gets a character.If you end up calling repeatedly this method, you should consider using
array()
instead.- Specified by:
charAt
in interfaceCharSequence
- Parameters:
index
- the index of a character.- Returns:
- the chracter at that index.
-
setCharAt
A nickname forcharAt(int,char)
.- Parameters:
index
- the index of a character.c
- the new character.- Returns:
- this mutable string.
- See Also:
-
charAt
Sets the character at the given index.If you end up calling repeatedly this method, you should consider using
array()
instead.- Parameters:
index
- the index of a character.c
- the new character.- Returns:
- this mutable string.
-
firstChar
public final char firstChar()Returns the first character of this mutable string.- Returns:
- the first character.
- Throws:
StringIndexOutOfBoundsException
- when called on the empty string.
-
lastChar
public final char lastChar()Returns the last character of this mutable string.- Returns:
- the last character.
- Throws:
ArrayIndexOutOfBoundsException
- when called on the empty string.
-
toCharArray
public final char[] toCharArray()Converts this string to a new character array.- Returns:
- a newly allocated character array with the same length and content of this mutable string.
-
substring
Returns a substring of this mutable string.The creation of a substring implies the creation of a new backing array. The returned mutable string will be compact.
- Parameters:
start
- first character of the substring (inclusive).end
- last character of the substring (exclusive).- Returns:
- a substring defined as above.
-
substring
Returns a substring of this mutable string.- Parameters:
start
- first character of the substring (inclusive).- Returns:
- a substring ranging from the given position to the end of this string.
- See Also:
-
subSequence
Returns a subsequence of this mutable string.Subsequences share the backing array. Thus, you should not use a subsequence after changing the characters of this mutable string between
start
andend
.Equality of
CharSequence
s returned by this method is defined by content equality, and hash codes are identical to mutable strings with the same content. Thus, you can mix mutable strings andCharSequence
s returned by this method in data structures, as the contracts ofequals()
andhashCode()
are honoured.- Specified by:
subSequence
in interfaceCharSequence
- Parameters:
start
- first character of the subsequence (inclusive).end
- last character of the subsequence (exclusive).- Returns:
- a subsequence defined as above.
- See Also:
-
append
Appends the given mutable string to this mutable string.- Parameters:
s
- the mutable string to append.- Returns:
- this mutable string.
-
append
Appends the givenString
to this mutable string.- Parameters:
s
- aString
(null
is not allowed).- Returns:
- this mutable string.
- Throws:
NullPointerException
- if the argument isnull
-
append
Appends the givenCharSequence
to this mutable string.- Specified by:
append
in interfaceAppendable
- Parameters:
s
- aCharSequence
ornull
.- Returns:
- this mutable string.
- See Also:
-
append
Appends a subsequence of the givenCharSequence
to this mutable string. Warning: the semantics of this method of that ofappend(char[], int, int)
are different.- Specified by:
append
in interfaceAppendable
- Parameters:
s
- aCharSequence
ornull
.start
- the index of the first character of the subsequence to append.end
- the index of the character after the last character in the subsequence.- Returns:
- this mutable string.
- See Also:
-
append
Appends the given character sequences to this mutable string using the given separator.- Parameters:
a
- an array.offset
- the index of the first character sequence to append.length
- the number of character sequences to append.separator
- a separator that will be appended inbetween the character sequences.- Returns:
- this mutable string.
-
append
Appends the given character sequences to this mutable string using the given separator.- Parameters:
a
- an array.separator
- a separator that will be appended inbetween the character sequences.- Returns:
- this mutable string.
-
append
Appends the string representations of the given objects to this mutable string using the given separator.- Parameters:
a
- an array of objects.offset
- the index of the first object to append.length
- the number of objects to append.separator
- a separator that will be appended inbetween the string representations of the given objects.- Returns:
- this mutable string.
-
append
Appends the string representations of the given objects to this mutable string using the given separator.- Parameters:
a
- an array of objects.separator
- a separator that will be appended inbetween the string representations of the given objects.- Returns:
- this mutable string.
-
append
Appends the given character array to this mutable string.- Parameters:
a
- an array to append.- Returns:
- this mutable string.
-
append
Appends a part of the given character array to this mutable string. Warning: the semantics of this method of that ofappend(CharSequence, int, int)
are different.- Parameters:
a
- an array.offset
- the index of the first character to append.len
- the number of characters to append.- Returns:
- this mutable string.
-
append
Appends the given character list to this mutable string.- Parameters:
list
- the list to append.- Returns:
- this mutable string.
-
append
Appends a part of the given character list to this mutable string. Warning: the semantics of this method of that ofappend(CharSequence, int, int)
are different.- Parameters:
list
- a character list.offset
- the index of the first character to append.len
- the number of characters to append.- Returns:
- this mutable string.
-
append
Appends a boolean to this mutable string.- Parameters:
b
- the boolean to be appended.- Returns:
- this mutable string.
-
append
Appends a character to this mutable string.Note that this method will reallocate the backing array of a compact mutable string for each character appended. Do not call it lightly.
- Specified by:
append
in interfaceAppendable
- Parameters:
c
- a character.- Returns:
- this mutable string.
-
append
Appends an integer to this mutable string.- Parameters:
i
- the integer to be appended.- Returns:
- this mutable string.
-
append
Appends a long to this mutable string.- Parameters:
l
- the long to be appended.- Returns:
- this mutable string.
-
append
Appends a float to this mutable string.- Parameters:
f
- the float to be appended.- Returns:
- this mutable string.
-
append
Appends a double to this mutable string.- Parameters:
d
- the double to be appended.- Returns:
- this mutable string.
-
append
Appends the string representation of an object to this mutable string.- Parameters:
o
- the object to append.- Returns:
- a reference to this mutable string.
-
insert
Inserts a mutable string in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.s
- the mutable string to be inserted.- Returns:
- this mutable string.
- Throws:
NullPointerException
- ifs
isnull
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts aString
in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.s
- theString
to be inserted.- Returns:
- this mutable string.
- Throws:
NullPointerException
- ifs
isnull
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts aCharSequence
in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theCharSequence
.s
- theCharSequence
to be inserted.- Returns:
- this mutable string.
- Throws:
NullPointerException
- ifs
isnull
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts characters in this mutable string. All of the characters of the arrayc
are inserted in this mutable string, and the first inserted character is going to have indexindex
.- Parameters:
index
- position at which to insert subarray.c
- the character array.- Returns:
- this mutable string.
- Throws:
NullPointerException
- ifc
isnull
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts characters in this mutable string.len
characters of the arrayc
, with indices starting fromoffset
, are inserted in this mutable string, and the first inserted character is going to have indexindex
.- Parameters:
index
- position at which to insert subarray.c
- the character array.offset
- the index of the first character ofc
to to be inserted.len
- the number of characters ofc
to to be inserted.- Returns:
- this mutable string.
- Throws:
NullPointerException
- ifc
isnull
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
, oroffset
orlen
are negative, oroffset+len
is greater thanc.length
.
-
insert
Inserts a boolean in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.b
- the boolean to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts a char in this mutable string, starting from indexindex
.Note that this method will not expand the capacity of a compact mutable string by more than one character. Do not call it lightly.
- Parameters:
index
- position at which to insert theString
.c
- the char to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts a double in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.d
- the double to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts a float in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.f
- the float to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts an int in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.x
- the int to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts a long in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.l
- the long to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
insert
Inserts the string representation of an object in this mutable string, starting from indexindex
.- Parameters:
index
- position at which to insert theString
.o
- the object to be inserted.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
.
-
delete
Removes the characters of this mutable string with indices in the range fromstart
(inclusive) toend
(exclusive). Ifend
is greater than or equal to the length of this mutable string, all characters with indices greater than or equal tostart
are deleted.- Parameters:
start
- The beginning index (inclusive).end
- The ending index (exclusive).- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifstart
is greater thanlength()
, or greater thanend
.
-
deleteCharAt
Removes the character at the given index.Note that this method will reallocate the backing array of a compact mutable string for each character deleted. Do not call it lightly.
- Parameters:
index
- Index of character to remove- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifindex
is negative, or greater than or equal tolength()
.
-
delete
Removes all occurrences of the given character.- Parameters:
c
- the character to remove.- Returns:
- this mutable string.
-
delete
Removes all occurrences of the given characters.- Parameters:
s
- the set of characters to remove.- Returns:
- this mutable string.
-
delete
Removes all occurrences of the given characters.- Parameters:
c
- an array containing the characters to remove.- Returns:
- this mutable string.
-
replace
Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the given mutable string.- Parameters:
start
- The starting index (inclusive).end
- The ending index (exclusive).s
- The mutable string to be copied.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifstart
is negative, greater thanlength()
, or greater thanend
.
-
replace
Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the givenString
.- Parameters:
start
- The starting index (inclusive).end
- The ending index (exclusive).s
- TheString
to be copied.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifstart
is negative, greater thanlength()
, or greater thanend
.
-
replace
Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the givenCharSequence
.- Parameters:
start
- The starting index (inclusive).end
- The ending index (exclusive).s
- TheCharSequence
to be copied.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifstart
is negative, greater thanlength()
, or greater thanend
.
-
replace
Replaces the characters with indices ranging fromstart
(inclusive) toend
(exclusive) with the given character.- Parameters:
start
- The starting index (inclusive).end
- The ending index (exclusive).c
- The character to be copied.- Returns:
- this mutable string.
- Throws:
IndexOutOfBoundsException
- ifstart
is negative, greater thanlength()
, or greater thanend
.
-
replace
Replaces the content of this mutable string with the given mutable string.- Parameters:
s
- the mutable string whose content will replace the present one.- Returns:
- this mutable string.
-
replace
Replaces the content of this mutable string with the given string.- Parameters:
s
- the string whose content will replace the present one.- Returns:
- this mutable string.
-
replace
Replaces the content of this mutable string with the given character sequence.- Parameters:
s
- the character sequence whose content will replace the present one.- Returns:
- this mutable string.
-
replace
Replaces the content of this mutable string with the given character.- Parameters:
c
- the character whose content will replace the present content.- Returns:
- this mutable string.
-
replace
Replaces each occurrence of a set of characters with a corresponding mutable string.Each occurrences of the character
c[i]
in this mutable string will be replaced bys[i]
. Note thatc
ands
must have the same length, and thatc
must not contain duplicates. Moreover, each replacement string must be nonempty.This method uses a Bloom filter to avoid repeated linear scans of the character array for
length()
times; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down replacements with more than one hundred characters.This method will try at most one reallocation.
- Parameters:
c
- an array of characters to be replaced.s
- an array of replacement mutable strings.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if one of the replacement strings is empty.
-
replace
Replaces each occurrence of a set of characters with a corresponding string.Each occurrences of the character
c[i]
in this mutable string will be replaced bys[i]
. Note thatc
ands
must have the same length, and thatc
must not contain duplicates. Moreover, each replacement string must be nonempty.This method uses a Bloom filter to avoid repeated linear scans of the character array for
length()
times; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down replacements with more than one hundred characters.This method will try at most one reallocation.
- Parameters:
c
- an array of characters to be replaced.s
- an array of replacement strings.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if one of the replacement strings is empty.
-
replace
Replaces each occurrence of a set of characters with a corresponding character sequence.Each occurrences of the character
c[i]
in this mutable string will be replaced bys[i]
. Note thatc
ands
must have the same length, and thatc
must not contain duplicates. Moreover, each replacement sequence must be nonempty.This method uses a Bloom filter to avoid repeated linear scans of the character array for
length()
times; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down replacements with more than one hundred characters.This method will try at most one reallocation.
- Parameters:
c
- an array of characters to be replaced.s
- an array of replacement character sequences.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if one of the replacement sequences is empty.
-
replace
Replaces each occurrence of a set characters with a corresponding character.Each occurrences of the character
c[i]
in this mutable string will be replaced byr[i]
. Note thatc
ands
must have the same length, and thatc
must not contain duplicates.This method uses a Bloom filter to avoid repeated linear scans of the character array for
length()
times; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down replacements with more than one hundred characters.- Parameters:
c
- an array of characters to be replaced.r
- an array of replacement characters.- Returns:
- this mutable string.
-
replace
Replaces characters following a replacement map.Each occurrence of a key of
m
will be substituted with the corresponding value.- Parameters:
m
- a map specifiying the character replacements.- Returns:
- this mutable string.
-
replace
Replaces each occurrence of a character with a corresponding mutable string.Each occurrences of the character
c
in this mutable string will be replaced bys
. Note thats
must be nonempty.This method will try at most one reallocation.
- Parameters:
c
- a character to be replaced.s
- a replacement mutable string.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if the replacement string is empty.
-
replace
Replaces each occurrence of a character with a corresponding string.Each occurrences of the character
c
in this mutable string will be replaced bys
. Note thats
must be nonempty.This method will try at most one reallocation.
- Parameters:
c
- a character to be replaced.s
- a replacement string.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if the replacement sequence is empty.
-
replace
Replaces each occurrence of a character with a corresponding character sequence.Each occurrences of the character
c
in this mutable string will be replaced bys
. Note thats
must be nonempty.This method will try at most one reallocation.
- Parameters:
c
- a character to be replaced.s
- a replacement character sequence.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if the replacement sequence is empty.
-
replace
Replaces each occurrence of a character with a corresponding character.- Parameters:
c
- a character to be replaced.r
- a replacement character.- Returns:
- this mutable string.
-
replace
Replaces each occurrence of a mutable string with a corresponding mutable string.Each occurrences of the mutable string
s
in this mutable string will be replaced byr
. Note thats
must be nonempty, unlessr
is empty, too.If the replacement string is longer than the search string, occurrences of the search string are matched from the end of this mutable string (i.e., using
lastIndexOf()
). Otherwise, occurrences of the search string are matched from the start (i.e., usingindexOf()
). This has no effect on the semantics, unless there are overlapping occurrences.This method will try at most one reallocation.
- Parameters:
s
- a mutable string to be replaced.r
- a replacement mutable string.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if you try to replace the empty string with a nonempty string.
-
replace
Replaces each occurrence of a string with a corresponding string.Each occurrences of the string
s
in this mutable string will be replaced byr
. Note thats
must be nonempty, unlessr
is empty, too.This method will try at most one reallocation.
- Parameters:
s
- a string to be replaced.r
- a replacement string.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if you try to replace the empty string with a nonempty string.- See Also:
-
replace
Replaces each occurrence of a character sequence with a corresponding character sequence.Each occurrences of the string
s
in this mutable string will be replaced byr
. Note thats
must be nonempty, unlessr
is empty, too.This method will try at most one reallocation.
- Parameters:
s
- a character sequence to be replaced.r
- a replacement character sequence.- Returns:
- this mutable string.
- Throws:
IllegalArgumentException
- if you try to replace the empty sequence with a nonempty sequence.- See Also:
-
indexOf
public final int indexOf(char c) Returns the index of the first occurrence of the specified character.- Parameters:
c
- the character to look for.- Returns:
- the index of the first occurrence of
c
, or-1
, if the character never appears.
-
indexOf
public final int indexOf(char c, int from) Returns the index of the first occurrence of the specified character, starting at the specified index.- Parameters:
c
- the character to look for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of
c
, or-1
, if the character never appears with index greater than or equal tofrom
.
-
indexOf
Returns the index of the first occurrence of the specified mutable string, starting at the specified index.This method uses a lightweight combination of Boyer–Moore's last-character heuristics and Bloom filters. More precisely, instead of recording the last occurrence of all characters in the pattern, we simply record in a small Bloom filter which characters belong to the pattern. Every time there is a mismatch, we look at the character immediately following the pattern: if it is not in the Bloom filter, besides moving to the next position we can additionally skip a number of characters equal to the length of the pattern.
Unless called with a pattern that saturates the filter, this method will usually outperform that of
String
.- Parameters:
pattern
- the mutable string to look for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of
pattern
after the firstfrom
characters, or-1
, if the string never appears.
-
indexOf
Returns the index of the first occurrence of the specified mutable string.- Parameters:
pattern
- the mutable string to look for.- Returns:
- the index of the first occurrence of
pattern
, or-1
, if the string never appears. - See Also:
-
indexOf
Returns the index of the first occurrence of the specified character sequence, starting at the specified index.Searching for a character sequence is slightly slower than
searching for a mutable string
, as every character of the pattern must be accessed through a method call. Please consider wrappingpattern
in a mutable string, or useTextPattern
for repeated searches.- Parameters:
pattern
- the character sequence to look for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of
pattern
after the firstfrom
characters, or-1
, if the string never appears. - See Also:
-
indexOf
Returns the index of the first occurrence of the specified character sequence.- Parameters:
pattern
- the character sequence to look for.- Returns:
- the index of the first occurrence of
pattern
, or-1
, if the string never appears. - See Also:
-
indexOf
Returns the index of the first occurrence of the specified text pattern, starting at the specified index.To use this method, you have to create a text pattern first.
- Parameters:
pattern
- a compiled text pattern to be searched for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of
pattern
, or-1
, if no such character ever appears.
-
indexOf
Returns the index of the first occurrence of the specified text pattern, starting at the specified index.- Parameters:
pattern
- a compiled text pattern to be searched for.- Returns:
- the index of the first occurrence of
pattern
, or-1
, if no such character ever appears. - See Also:
-
indexOfAnyOf
Returns the index of the first occurrence of any of the specified characters, starting at the specified index.- Parameters:
s
- a set of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of any of the characters in
s
, or-1
, if no such character ever appears.
-
indexOfAnyOf
Returns the index of the first occurrence of any of the specified characters.- Parameters:
s
- a set of characters to be searched for.- Returns:
- the index of the first occurrence of any of the characters in
s
, or-1
, if no such character ever appears. - See Also:
-
indexOfAnyOf
public int indexOfAnyOf(char[] c, int from) Returns the index of the first occurrence of any of the specified characters, starting at the specified index.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
c
- an array of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of any of the characters in
c
, or-1
, if no such character ever appears.
-
indexOfAnyOf
public int indexOfAnyOf(char[] c) Returns the index of the first occurrence of any of the specified characters.- Parameters:
c
- an array of characters to be searched for.- Returns:
- the index of the first occurrence of any of the characters in
c
, or-1
, if no such character ever appears. - See Also:
-
indexOfAnyBut
Returns the index of the first occurrence of any character, except those specified, starting at the specified index.- Parameters:
s
- a set of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of any of the characters not in
s
, or-1
, if no such character ever appears.
-
indexOfAnyBut
Returns the index of the first occurrence of any character, except those specified.- Parameters:
s
- a set of characters to be searched for.- Returns:
- the index of the first occurrence of any of the characters not in
s
, or-1
, if no such character ever appears. - See Also:
-
indexOfAnyBut
public int indexOfAnyBut(char[] c, int from) Returns the index of the first occurrence of any character, except those specified, starting at the specified index.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
c
- an array of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the first occurrence of any of the characters not in
c
, or-1
, if no such character ever appears.
-
indexOfAnyBut
public int indexOfAnyBut(char[] c) Returns the index of the first occurrence of any character, except those specified.- Parameters:
c
- an array of characters to be searched for.- Returns:
- the index of the first occurrence of any of the characters not in
c
, or-1
, if no such character ever appears. - See Also:
-
lastIndexOf
public final int lastIndexOf(char c) Returns the index of the last occurrence of the specified character.- Parameters:
c
- the character to look for.- Returns:
- the index of the last occurrence of
c
, or-1
, if the character never appears.
-
lastIndexOf
public final int lastIndexOf(char c, int from) Returns the index of the last occurrence of the specified character, searching backward starting at the specified index.- Parameters:
c
- the character to look for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of
c
, or-1
, if the character never appears with index smaller than or equal tofrom
.
-
lastIndexOf
Returns the index of the last occurrence of the specified mutable string, searching backward starting at the specified index.- Parameters:
pattern
- the mutable string to look for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of
pattern
, or-1
, if thepattern
never appears with index smaller than or equal tofrom
.
-
lastIndexOf
Returns the index of the last occurrence of the specified mutable string.- Parameters:
pattern
- the mutable string to look for.- Returns:
- the index of the last occurrence of
pattern
, or-1
, if thepattern
never appears. - See Also:
-
lastIndexOf
Returns the index of the last occurrence of the specified character sequence, searching backward starting at the specified index.- Parameters:
pattern
- the character sequence to look for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of
pattern
, or-1
, if thepattern
never appears with index smaller than or equal tofrom
. - See Also:
-
lastIndexOf
Returns the index of the last occurrence of the specified character sequence.- Parameters:
pattern
- the character sequence to look for.- Returns:
- the index of the last occurrence of
pattern
, or-1
, if thepattern
never appears. - See Also:
-
lastIndexOfAnyOf
Returns the index of the last occurrence of any of the specified characters, searching backwards starting at the specified index.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
s
- a set of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of any character in
s
, or-1
, if no character ins
ever appears with index smaller than or equal tofrom
.
-
lastIndexOfAnyOf
Returns the index of the last occurrence of any of the specified characters.- Parameters:
s
- a set of characters to be searched for.- Returns:
- the index of the last occurrence of any of the characters in
s
, or-1
, if no such character ever appears. - See Also:
-
lastIndexOfAnyOf
public int lastIndexOfAnyOf(char[] c, int from) Returns the index of the last occurrence of any of the specified characters, searching backwards starting at the specified index.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
c
- an array of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of any character in
c
, or-1
, if no character inc
ever appears with index smaller than or equal tofrom
.
-
lastIndexOfAnyOf
public int lastIndexOfAnyOf(char[] c) Returns the index of the last occurrence of any of the specified characters.- Parameters:
c
- an array of characters to be searched for.- Returns:
- the index of the last occurrence of any of the characters in
c
, or-1
, if no such character ever appears. - See Also:
-
lastIndexOfAnyBut
Returns the index of the last occurrence of any character, except those specified, starting at the specified index.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
s
- a set of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of any of the characters not in
c
, or-1
, if no such character ever appears.
-
lastIndexOfAnyBut
Returns the index of the last occurrence of any character, except those specified.- Parameters:
s
- a set of characters to be searched for.- Returns:
- the index of the last occurrence of any of the characters not in
s
, or-1
, if no such character ever appears. - See Also:
-
lastIndexOfAnyBut
public int lastIndexOfAnyBut(char[] c, int from) Returns the index of the last occurrence of any character, except those specified, starting at the specified index.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
c
- an array of characters to be searched for.from
- the index from which the search must start.- Returns:
- the index of the last occurrence of any of the characters not in
c
, or-1
, if no such character ever appears.
-
lastIndexOfAnyBut
public int lastIndexOfAnyBut(char[] c) Returns the index of the last occurrence of any character, except those specified.- Parameters:
c
- an array of characters to be searched for.- Returns:
- the index of the last occurrence of any of the characters not in
c
, or-1
, if no such character ever appears. - See Also:
-
span
Spans a segment of this mutable string made of the specified characters.- Parameters:
s
- a set of characters.from
- the index from which to span.- Returns:
- the length of the maximal subsequence of this mutable string made of
characters in
s
starting atfrom
. - See Also:
-
span
Spans the initial segment of this mutable string made of the specified characters.- Parameters:
s
- a set of characters.- Returns:
- the length of the maximal initial subsequence of this mutable string made of
characters in
s
. - See Also:
-
span
public int span(char[] c, int from) Spans a segment of this mutable string made of the specified characters.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
c
- an array of characters.from
- the index from which to span.- Returns:
- the length of the maximal subsequence of this mutable string made of
characters in
c
starting atfrom
. - See Also:
-
span
public int span(char[] c) Spans the initial segment of this mutable string made of the specified characters.- Parameters:
c
- an array of characters.- Returns:
- the length of the maximal initial subsequence of this mutable string made of
characters in
c
. - See Also:
-
cospan
Spans a segment of this mutable string made of the complement of the specified characters.- Parameters:
s
- a set of characters.from
- the index from which to span.- Returns:
- the length of the maximal subsequence of this mutable string made of
characters not in
s
starting atfrom
. - See Also:
-
cospan
Spans the initial segment of this mutable string made of the complement of the specified characters.- Parameters:
s
- a set of characters.- Returns:
- the length of the maximal initial subsequence of this mutable string made of
characters not in
s
. - See Also:
-
cospan
public int cospan(char[] c, int from) Spans a segment of this mutable string made of the complement of the specified characters.This method uses a Bloom filter to avoid repeated linear scans of the array
c
; however, this optimisation will be most effective with arrays of less than twenty characters, and, in fact, will very slightly slow down searches for more than one hundred characters.- Parameters:
c
- an array of characters.from
- the index from which to span.- Returns:
- the length of the maximal subsequence of this mutable string made of
characters not in
c
starting atfrom
. - See Also:
-
cospan
public int cospan(char[] c) Spans the initial segment of this mutable string made of the complement of the specified characters.- Parameters:
c
- an array of characters.- Returns:
- the length of the maximal initial subsequence of this mutable string made of
characters not in
c
. - See Also:
-
startsWith
Returns whether this mutable string starts with the given mutable string.- Parameters:
prefix
- a mutable string.- Returns:
true
if this mutable string starts withprefix
.
-
startsWith
Returns whether this mutable string starts with the given string.- Parameters:
prefix
- a string.- Returns:
true
if this mutable string starts withprefix
.
-
startsWith
Returns whether this mutable string starts with the given character sequence.- Parameters:
prefix
- a character sequence.- Returns:
true
if this mutable string starts withprefix
.
-
startsWithIgnoreCase
Returns whether this mutable string starts with the given mutable string disregarding case.- Parameters:
prefix
- a mutable string.- Returns:
true
if this mutable string starts withprefix
up to case.
-
startsWithIgnoreCase
Returns whether this mutable string starts with the given string disregarding case.- Parameters:
prefix
- a string.- Returns:
true
if this mutable string starts withprefix
up to case.
-
startsWithIgnoreCase
Returns whether this mutable string starts with the given character sequence disregarding case.- Parameters:
prefix
- a character sequence.- Returns:
true
if this mutable string starts withprefix
up to case.
-
endsWith
Returns whether this mutable string ends with the given mutable string.- Parameters:
suffix
- a mutable string.- Returns:
true
if this mutable string ends withsuffix
.
-
endsWith
Returns whether this mutable string ends with the given string.- Parameters:
suffix
- a string.- Returns:
true
if this mutable string ends withsuffix
.
-
endsWith
Returns whether this mutable string ends with the given character sequence.- Parameters:
suffix
- a character sequence.- Returns:
true
if this mutable string ends withprefix
.
-
endsWithIgnoreCase
Returns whether this mutable string ends with the given mutable string disregarding case.- Parameters:
suffix
- a mutable string.- Returns:
true
if this mutable string ends withsuffix
up to case.
-
endsWithIgnoreCase
Returns whether this mutable string ends with the given string disregarding case.- Parameters:
suffix
- a string.- Returns:
true
if this mutable string ends withsuffix
up to case.
-
endsWithIgnoreCase
Returns whether this mutable string ends with the given character sequence disregarding case.- Parameters:
suffix
- a character sequence.- Returns:
true
if this mutable string ends withprefix
up to case.
-
toLowerCase
Converts all of the characters in this mutable string to lower case using the rules of the default locale.- Returns:
- this mutable string.
-
toUpperCase
Converts all of the characters in this mutable string to upper case using the rules of the default locale.- Returns:
- this mutable string.
-
trim
Trims all leading and trailing whitespace from this string. Whitespace here is any character smaller than' '
(the ASCII space).- Returns:
- this mutable string.
-
trimLeft
Trims all leading whitespace from this string. Whitespace here is any character smaller than' '
(the ASCII space).- Returns:
- this mutable string.
-
trimRight
Trims all trailing whitespace from this string. Whitespace here is any character smaller than' '
(the ASCII space).- Returns:
- this mutable string.
-
squeezeSpaces
Squeezes and normalizes spaces in this mutable string. All subsequences of consecutive characters satisfyingCharacter.isSpaceChar(char)
(orCharacter.isWhitespace(char)
ifsqueezeOnlyWhitespace
is true) will be transformed into a single space.- Parameters:
squeezeOnlyWhitespace
- if true, a space is defined byCharacter.isWhitespace(char)
; otherwise, a space is defined byCharacter.isSpaceChar(char)
.- Returns:
- this mutable string.
-
squeezeWhitespace
Squeezes and normalizes whitespace in this mutable string. All subsequences of consecutive characters satisfyingCharacter.isWhitespace(char)
will be transformed into a single space.- Returns:
- this mutable string.
- See Also:
-
squeezeSpace
Squeezes and normalizes spaces in this mutable string. All subsequences of consecutive characters satisfyingCharacter.isSpaceChar(char)
will be transformed into a single space.- Returns:
- this mutable string.
- See Also:
-
reverse
The characters in this mutable string get reversed.- Returns:
- this mutable string.
-
write
Writes this mutable string to aWriter
.- Parameters:
w
- aWriter
.- Throws:
IOException
- if thrown by the providedWriter
.
-
read
Reads a mutable string that has been written bywrite(Writer)
from aReader
.If
length
is smaller than the current capacity or the number of characters actually read is smaller thanlength
the string will become loose.- Parameters:
r
- aReader
.length
- the number of characters to read.- Returns:
- The number of characters read, or -1 if the end of the stream has been reached.
- Throws:
IOException
- if thrown by the providedReader
.
-
print
Prints this mutable string to aPrintWriter
.- Parameters:
w
- aPrintWriter
.
-
println
Prints this mutable string to aPrintWriter
and then terminates the line.- Parameters:
w
- aPrintWriter
.
-
print
Prints this mutable string to aPrintStream
.- Parameters:
s
- aPrintStream
.
-
println
Prints this mutable string to aPrintStream
and then terminates the line.- Parameters:
s
- aPrintStream
.
-
writeUTF8
Deprecated.This method will not process surrogate pairs correctly.Writes this mutable string in UTF-8 encoding.The string is coded in UTF-8, not in the Java modified UTF representation. Thus, an ASCII NUL is represented by a single zero. Watch out!
This method does not try to do any caching (in particular, it does not create any object). On non-buffered data outputs it might be very slow.
- Parameters:
s
- a data output.- Throws:
IOException
- ifs
does.
-
readUTF8
Deprecated.This method will not process surrogate pairs correctly.Reads a mutable string in UTF-8 encoding.This method does not try to do any read-ahead (in particular, it does not create any object). On non-buffered data inputs it might be very slow.
This method is able to read only strings containing UTF-8 sequences of at most 3 bytes. Longer sequences will cause a
UTFDataFormatException
.If
length
is smaller than the current capacity, the string will become loose.- Parameters:
s
- a data input.length
- the number of characters to read.- Returns:
- this mutable string.
- Throws:
UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- ifs
does.
-
writeSelfDelimUTF8
Writes this mutable string to aDataOutput
as a length followed by a UTF-8 encoding.The purpose of this method and of
readSelfDelimUTF8(DataInput)
is to provide a simple, ready-to-use (even if not particularly efficient) method for storing arbitrary mutable strings in a self-delimiting way.You can save any mutable string using this method. The length will be written in packed 7-bit format, that is, as a list of blocks of 7 bits (from higher to lower) in which the seventh bit determines whether there is another block in the list. For strings shorter than 27 characters, the length will be packed in one byte, for strings shorter than 214 in 2 bytes and so on.
The string following the length is coded in UTF-8, not in the Java modified UTF representation. Thus, an ASCII NUL is represented by a single zero. Note also that surrogate pairs will be written as such, that is, without coalescing them into a single codepoint. Watch out!
- Parameters:
s
- a data output.- Throws:
IOException
- ifs
does.
-
readSelfDelimUTF8
Reads a mutable string that has been written bywriteSelfDelimUTF8()
from aDataInput
.- Parameters:
s
- a data input.- Returns:
- this mutable string.
- Throws:
IOException
- ifs
does.- See Also:
-
writeUTF8
Deprecated.This method will not process surrogate pairs correctly.Writes this mutable string in UTF-8 encoding.- Parameters:
s
- an output stream.- Throws:
IOException
- ifs
does.- See Also:
-
skipSelfDelimUTF8
Skips a string encoded bywriteSelfDelimUTF8(OutputStream)
.- Parameters:
s
- an input stream.- Returns:
- the length of the skipped string.
- Throws:
UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- ifs
does, or we try to read beyond end-of-file.- See Also:
-
readUTF8
Deprecated.This method will not process surrogate pairs correctly.Reads a mutable string in UTF-8 encoding.- Parameters:
s
- an input stream.length
- the number of characters to read.- Returns:
- this mutable string.
- Throws:
UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- ifs
does, or we try to read beyond end-of-file.- See Also:
-
writeSelfDelimUTF8
Writes this mutable string to anOutputStream
as a length followed by a UTF-8 encoding.- Parameters:
s
- an output stream.- Throws:
IOException
- ifs
does.- See Also:
-
readSelfDelimUTF8
Reads a mutable string that has been written bywriteSelfDelimUTF8()
from anInputStream
.- Parameters:
s
- an input stream.- Returns:
- this mutable string.
- Throws:
UTFDataFormatException
- on UTF-8 sequences longer than three octects.IOException
- ifs
does.IOException
- ifs
does, or we try to read beyond end-of-file.- See Also:
-
equals
Compares this mutable string to another object.This method will return
true
iff its argument is aCharSequence
containing the same characters of this mutable string.A potentially nasty consequence is that equality is not symmetric. See the discussion in the class description.
-
equals
Type-specific version ofequals()
. This version of theequals(Object)
method will be called on mutable strings.- Parameters:
s
- a mutable string.- Returns:
- true if the two mutable strings contain the same characters.
- See Also:
-
equals
Type-specific version ofequals()
. This version of theequals(Object)
method will be called onString
s. It is guaranteed that it will returntrue
iff the mutable string and theString
contain the same characters. Thus, you can use expressions likemutableString.equals("Hello")
to check against string contants.- Parameters:
s
- aString
.- Returns:
- true if the
String
contain the same characters of this mutable string. - See Also:
-
equals
Type-specific version ofequals()
. This version of theequals(Object)
method will be called on character sequences. It is guaranteed that it will returntrue
iff this mutable string and the character sequence contain the same characters.- Parameters:
s
- a character sequence.- Returns:
- true if the character sequence contains the same characters of this mutable string.
- See Also:
-
equalsIgnoreCase
Checks two mutable strings for equality ignoring case.- Parameters:
s
- a mutable string.- Returns:
- true if the two mutable strings contain the same characters up to case.
- See Also:
-
equalsIgnoreCase
Type-specific version ofequalsIgnoreCase()
.- Parameters:
s
- a string.- Returns:
- true if the string contains the same characters of this mutable string up to case.
- See Also:
-
equalsIgnoreCase
Type-specific version ofequalsIgnoreCase()
.- Parameters:
s
- a character sequence.- Returns:
- true if the character sequence contains the same characters of this mutable string up to case.
- See Also:
-
compareTo
Compares this mutable string to another mutable string performing a lexicographical comparison.- Specified by:
compareTo
in interfaceComparable<MutableString>
- Parameters:
s
- a mutable string.- Returns:
- a negative integer, zero, or a positive integer as this mutable string is less than, equal to, or greater than the specified mutable string.
-
compareTo
Compares this mutable string to a string performing a lexicographical comparison.- Parameters:
s
- aString
.- Returns:
- a negative integer, zero, or a positive integer as this mutable
string is less than, equal to, or greater than the specified
String
.
-
compareTo
Compares this mutable string to a character sequence performing a lexicographical comparison.- Parameters:
s
- a character sequence.- Returns:
- a negative integer, zero, or a positive integer as this mutable string is less than, equal to, or greater than the specified character sequence.
-
compareToIgnoreCase
Compares this mutable string to another object disregarding case. If the argument is a character sequence, this method performs a lexicographical comparison; otherwise, it throws aClassCastException
.A potentially nasty consequence is that comparisons are not symmetric. See the discussion in the class description.
- Parameters:
s
- a mutable string.- Returns:
- a negative integer, zero, or a positive integer as this mutable string is less than, equal to, or greater than the specified mutable string once case differences have been eliminated.
- See Also:
-
compareToIgnoreCase
Type-specific version ofcompareToIgnoreCase()
.- Parameters:
s
- a mutable string.- Returns:
- a negative integer, zero, or a positive integer as this mutable string is less than, equal to, or greater than the specified string once case differences have been eliminated.
- See Also:
-
compareToIgnoreCase
Type-specific version ofcompareToIgnoreCase()
.- Parameters:
s
- a mutable string.- Returns:
- a negative integer, zero, or a positive integer as this mutable string is less than, equal to, or greater than the specified character sequence once case differences have been eliminated.
- See Also:
-
hashCode
public final int hashCode()Returns a hash code for this mutable string.The hash code of a mutable string is the same as that of a
String
with the same content, but with the leftmost bit set.A compact mutable string caches its hash code, so it is very efficient on data structures that check hash codes before invoking
equals()
. -
toString
- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-