Class GStringImpl
- All Implemented Interfaces:
Buildable,GroovyObject,Writable,Serializable,CharSequence,Comparable
- See Also:
-
Field Summary
Fields inherited from class groovy.lang.GString
EMPTY, EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY -
Constructor Summary
ConstructorsModifierConstructorDescriptionGStringImpl(Object[] values, String[] strings) Create a new GString with values and strings.protectedGStringImpl(Object[] values, String[] strings, boolean cacheable, String cachedStringLiteral, boolean frozen) Create a new GString with values and strings. -
Method Summary
Modifier and TypeMethodDescriptionvoidbuild(GroovyObject builder) Builds this object into the supplied builder.protected intCalculates the initial buffer capacity for rendering this GString.intcodePointAt(int index) Returns the Unicode code point at UTF-16 indexindexin the rendered text.intcodePointBefore(int index) Returns the Unicode code point that ends just before UTF-16 indexindex.intcodePointCount(int beginIndex, int endIndex) Returns how many Unicode code points lie in the rendered text frombeginIndexinclusive toendIndexexclusive.intOrders the rendered text againstanotherStringby UTF-16 code unit, using the same ordering asString.compareTo(String).intOrders the rendered text againststrby UTF-16 code unit after case folding.Returns the rendered text followed bystr.booleanReports whethersoccurs in the rendered text.booleanReports whether the rendered text andcshave the same length and the same characters.booleanReports whether the rendered text andsbhave the same length and the same characters.booleanReports whether the rendered text ends withsuffix.booleanequalsIgnoreCase(String anotherString) Reports whether the rendered text andanotherStringare equal ignoring case.freeze()byte[]Encodes the rendered text withcharsetinto a new byte array.voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies UTF-16 code units from the rendered text intodst.String[]Get the strings of this GString.Object[]Returns the interpolated values.intindexOf(int ch) Returns the UTF-16 index of the first occurrence of code pointchin the rendered text, or-1when it does not occur.intindexOf(int ch, int fromIndex) Returns the UTF-16 index of the first occurrence of code pointchat or afterfromIndex, or-1when it does not occur.intReturns the UTF-16 index of the first occurrence ofstrin the rendered text, or-1when it does not occur.intReturns the UTF-16 index of the first occurrence ofstrat or afterfromIndex, or-1when it does not occur.intern()Returns the pooled instance of the rendered text.booleanisBlank()booleanisEmpty()Reports whether the rendered text contains no code units.intlastIndexOf(int ch) Returns the UTF-16 index of the last occurrence of code pointchin the rendered text, or-1when it does not occur.intlastIndexOf(int ch, int fromIndex) Returns the UTF-16 index of the last occurrence of code pointchat or beforefromIndex, or-1when it does not occur.intlastIndexOf(String str) Returns the UTF-16 index of the last occurrence ofstrin the rendered text, or-1when it does not occur.intlastIndexOf(String str, int fromIndex) Returns the UTF-16 index of the last occurrence ofstrat or beforefromIndex, or-1when it does not occur.lines()Returns a stream of lines extracted from this string, separated by line terminators.booleanReports whetherregexmatches the whole rendered text.intoffsetByCodePoints(int index, int codePointOffset) Returns the UTF-16 index reached by movingcodePointOffsetcode points fromindex.Concatenates this GString with another GString.booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) CompareslenUTF-16 code units of the rendered text starting attoffsetwith the same number of units ofotherstarting atooffset.booleanregionMatches(int toffset, String other, int ooffset, int len) CompareslenUTF-16 code units of the rendered text starting attoffsetwithlencode units ofotherstarting atooffset.repeat(int count) Returns a string whose value is the concatenation of this string repeatedcounttimes.replace(char oldChar, char newChar) Returns the rendered text with everyoldCharreplaced bynewChar.replace(CharSequence target, CharSequence replacement) Returns the rendered text with every occurrence of the literal sequencetargetreplaced byreplacement.replaceAll(String regex, String replacement) Returns the rendered text with every substring matched byregexreplaced byreplacement.replaceFirst(String regex, String replacement) Returns the rendered text with the first substring matched byregexreplaced byreplacement.String[]Splits the rendered text around matches ofregexand drops trailing empty strings.String[]Splits the rendered text around matches ofregex.booleanstartsWith(String prefix) Reports whether the rendered text begins withprefix.booleanstartsWith(String prefix, int toffset) Reports whetherprefixoccurs in the rendered text at UTF-16 indextoffset.strip()Returns a string whose value is this string, with all leading and trailing white space removed.Returns a string whose value is this string, with all leading white space removed.Returns a string whose value is this string, with all trailing white space removed.substring(int beginIndex) Returns the tail of the rendered text frombeginIndexinclusive through the last code unit.substring(int beginIndex, int endIndex) Returns the slice of the rendered text frombeginIndexinclusive toendIndexexclusive.char[]Copies the UTF-16 code units of the rendered text into a new array.Returns the rendered text converted to lower case with the case-mapping rules ofLocale.getDefault()at the time of the call.toLowerCase(Locale locale) Returns the rendered text converted to lower case with the case-mapping rules oflocale.toString()Returns the rendered text converted to upper case with the case-mapping rules ofLocale.getDefault()at the time of the call.toUpperCase(Locale locale) Returns the rendered text converted to upper case with the case-mapping rules oflocale.trim()Returns the rendered text with every leading and trailing UTF-16 code unit whose value is at mostU+0020removed.Writes this object to the given writer.Methods inherited from class groovy.lang.GString
charAt, compareTo, equals, equals, getBytes, getBytes, getValue, getValueCount, hashCode, invokeMethod, length, negate, plus, subSequenceMethods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePointsMethods inherited from interface groovy.lang.GroovyObject
getProperty, setProperty
-
Constructor Details
-
GStringImpl
Create a new GString with values and strings.Each value is prefixed by a string, after the last value an additional String might be used, hence the following constraint is expected to hold:
strings.length == values.length || strings.length == values.length + 1.NOTE: The lengths are not checked but using arrays with lengths which violate the above constraint could result in unpredictable behaviour.
- Parameters:
values- the value partsstrings- the string parts
-
GStringImpl
protected GStringImpl(Object[] values, String[] strings, boolean cacheable, String cachedStringLiteral, boolean frozen) Create a new GString with values and strings.Each value is prefixed by a string, after the last value an additional String might be used, hence the following constraint is expected to hold:
strings.length == values.length || strings.length == values.length + 1.NOTE: The lengths are not checked but using arrays with lengths which violate the above constraint could result in unpredictable behaviour.
- Parameters:
values- the value partsstrings- the string partsfrozen- creates a GStringImpl which is not subject to mutation and hence more amenable to caching
-
-
Method Details
-
plus
Description copied from class:GStringConcatenates this GString with another GString. -
writeTo
Description copied from class:GStringWrites this object to the given writer.This is used to defer content creation until the point when it is streamed to the output destination. Oftentimes, content will be defined but not necessarily created (as it may be the case with a Closure definition.) In that case, the output is then 'deferred' to the point when it is serialized to the writer. This class may be used whenever an object should be responsible for creating its own textual representation, but creating the entire output as a single String would be inefficient (such as outputting a multi-gigabyte XML document.)
- Specified by:
writeToin interfaceWritable- Overrides:
writeToin classGString- Parameters:
out- the Writer to which this Writable should output its data.- Returns:
- the Writer that was passed
- Throws:
IOException- if an error occurred while outputting data to the writer
-
build
Description copied from class:GStringBuilds this object into the supplied builder. -
calcInitialCapacity
protected int calcInitialCapacity()Description copied from class:GStringCalculates the initial buffer capacity for rendering this GString.- Overrides:
calcInitialCapacityin classGString- Returns:
- the initial capacity
-
freeze
- Returns:
- returns an equivalent optimised but less mutable version of this GString
-
getStrings
Get the strings of this GString.This method returns the same array as used in the constructor. Changing the values will result in changes of the GString. It is generally not recommended to do so.
- Specified by:
getStringsin classGString- Returns:
- the string segments
-
getValues
Description copied from class:GStringReturns the interpolated values. -
trim
Returns the rendered text with every leading and trailing UTF-16 code unit whose value is at mostU+0020removed. When neither end has a code unit at or below that bound, the rendered string is returned unchanged.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Returns:
- the rendered text without those leading and trailing code units
-
isEmpty
public boolean isEmpty()Reports whether the rendered text contains no code units.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Returns:
truewhen the rendered length is zero
-
lines
Returns a stream of lines extracted from this string, separated by line terminators.A line terminator is one of the following: a line feed character
"\n"(U+000A), a carriage return character"\r"(U+000D), or a carriage return followed immediately by a line feed"\r\n"(U+000D U+000A).A line is either a sequence of zero or more characters followed by a line terminator, or it is a sequence of one or more characters followed by the end of the string. A line does not include the line terminator.
The stream returned by this method contains the lines from this string in the order in which they occur.
API Note: This definition of line implies that an empty string has zero lines and that there is no empty line following a line terminator at the end of a string.
Implementation Note: This method provides better performance than
split("\R")by supplying elements lazily and by faster search of new line terminators.- Returns:
- the stream of lines extracted from this string
- Since:
- 5.0.0
-
isBlank
public boolean isBlank()- Returns:
trueif the string is empty or contains only white space codepoints, otherwisefalse- Since:
- 5.0.0
- See Also:
-
repeat
Returns a string whose value is the concatenation of this string repeatedcounttimes.If this string is empty or count is zero then the empty string is returned.
- Parameters:
count- number of times to repeat- Returns:
- A string composed of this string repeated
counttimes or the empty string if this string is empty or count is zero - Throws:
IllegalArgumentException- if thecountis negative.- Since:
- 5.0.0
-
stripLeading
Returns a string whose value is this string, with all leading white space removed.If this
Stringobject represents an empty string, or if all code points in this string are white space, then an empty string is returned.Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to and including the last code point of this string.
This method may be used to trim white space from the beginning of a string.
- Returns:
- a string whose value is this string, with all leading white space removed
- Since:
- 5.0.0
- See Also:
-
stripTrailing
Returns a string whose value is this string, with all trailing white space removed.If this
Stringobject represents an empty string, or if all characters in this string are white space, then an empty string is returned.Otherwise, returns a substring of this string beginning with the first code point of this string up to and including the last code point that is not a white space.
This method may be used to trim white space from the end of a string.
- Returns:
- a string whose value is this string, with all trailing white space removed
- Since:
- 5.0.0
- See Also:
-
strip
Returns a string whose value is this string, with all leading and trailing white space removed.If this
Stringobject represents an empty string, or if all code points in this string are white space, then an empty string is returned.Otherwise, returns a substring of this string beginning with the first code point that is not a white space up to and including the last code point that is not a white space.
This method may be used to strip white space from the beginning and end of a string.
- Returns:
- a string whose value is this string, with all leading and trailing white space removed
- Since:
- 5.0.0
- See Also:
-
codePointAt
public int codePointAt(int index) Returns the Unicode code point at UTF-16 indexindexin the rendered text. A high surrogate at that index followed by a low surrogate is returned as one supplementary code point; any other code unit, including an unpaired surrogate, is returned as its own value.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsStringIndexOutOfBoundsExceptionifindexis negative or not less than the rendered length.- Parameters:
index- UTF-16 index of the code unit to read, counting from zero- Returns:
- the code point that starts at
index
-
codePointBefore
public int codePointBefore(int index) Returns the Unicode code point that ends just before UTF-16 indexindex. When the code unit atindex - 1is a low surrogate and the unit before it is a high surrogate, the supplementary code point of that pair is returned; otherwise the single code unit atindex - 1is returned.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsStringIndexOutOfBoundsExceptionifindexis less than 1 or greater than the rendered length.- Parameters:
index- UTF-16 index just after the code point to read- Returns:
- the code point that ends at
index
-
codePointCount
public int codePointCount(int beginIndex, int endIndex) Returns how many Unicode code points lie in the rendered text frombeginIndexinclusive toendIndexexclusive. An unpaired surrogate counts as one code point.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsIndexOutOfBoundsExceptionifbeginIndexis negative,endIndexis greater than the rendered length, orbeginIndexis greater thanendIndex.- Parameters:
beginIndex- first UTF-16 index of the range, inclusiveendIndex- UTF-16 index at which the range stops, exclusive- Returns:
- the number of code points in that range
-
offsetByCodePoints
public int offsetByCodePoints(int index, int codePointOffset) Returns the UTF-16 index reached by movingcodePointOffsetcode points fromindex. An unpaired surrogate counts as one code point.indexmay equal the rendered length, and a zero offset then stays on that length.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsIndexOutOfBoundsExceptionifindexis negative or greater than the rendered length, or if the walk would pass either end of the text.- Parameters:
index- UTF-16 index at which the walk starts, from zero through the rendered length inclusivecodePointOffset- number of code points to move. Positive moves toward the end and negative moves toward the start- Returns:
- the UTF-16 index where the walk stops
-
getChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Copies UTF-16 code units from the rendered text intodst. The copied range starts atsrcBegininclusive and stops atsrcEndexclusive, and the first copied unit is written atdstBegin.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifdstis null. ThrowsStringIndexOutOfBoundsExceptionif the source range falls outside the rendered text, or ifdsthas no room for the copied units starting atdstBegin.- Parameters:
srcBegin- first rendered index to copy, inclusivesrcEnd- rendered index at which copying stops, exclusivedst- array that receives the copied code unitsdstBegin- index indstof the first copied unit
-
getBytes
Encodes the rendered text withcharsetinto a new byte array.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifcharsetis null.- Parameters:
charset- encoding used to turn the rendered text into bytes- Returns:
- a new array holding that encoding
-
contentEquals
Reports whether the rendered text andsbhave the same length and the same characters. The buffer is locked while its length and characters are read.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifsbis null.- Parameters:
sb- buffer whose current contents are compared- Returns:
truewhen both sides have the same length and characters
-
contentEquals
Reports whether the rendered text andcshave the same length and the same characters. AStringis compared withString.equals(Object). AStringBufferis locked for the read. AStringBuilderis compared without a lock. Any other sequence is walked with its length andcharAt.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifcsis null.- Parameters:
cs- sequence compared with the rendered text- Returns:
truewhen both sides have the same length and characters
-
equalsIgnoreCase
Reports whether the rendered text andanotherStringare equal ignoring case. The lengths must already match, and each UTF-16 code unit is then folded withCharacter.toUpperCase(char)and, when those differ,Character.toLowerCase(char). No locale is consulted, so the result can disagree with folding both sides throughString.toLowerCase(). A null argument yieldsfalse.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Parameters:
anotherString- string compared with the rendered text- Returns:
truewhen the two texts match under that case folding
-
compareTo
Orders the rendered text againstanotherStringby UTF-16 code unit, using the same ordering asString.compareTo(String).The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifanotherStringis null.- Parameters:
anotherString- string the rendered text is ordered against- Returns:
- a negative integer, zero, or a positive integer when the
rendered text is less than, equal to, or greater than
anotherString
-
compareToIgnoreCase
Orders the rendered text againststrby UTF-16 code unit after case folding. Each unit is folded withCharacter.toUpperCase(char)and thenCharacter.toLowerCase(char). No locale is consulted. When one folded text is a prefix of the other, the shorter one sorts first.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifstris null.- Parameters:
str- string the rendered text is ordered against- Returns:
- a negative integer, zero, or a positive integer when the
folded rendered text is less than, equal to, or greater than
the folded
str
-
regionMatches
CompareslenUTF-16 code units of the rendered text starting attoffsetwithlencode units ofotherstarting atooffset. A negative offset, or a region that does not fit in either text, yieldsfalse. A negativelencompares no units and yieldstruewhen both offsets are non-negative.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifotheris null.- Parameters:
toffset- first rendered index of the regionother- string that supplies the region to compare againstooffset- first index of the region inotherlen- number of UTF-16 code units to compare- Returns:
truewhen the compared units match, or whenlenis negative and both offsets are non-negative
-
regionMatches
CompareslenUTF-16 code units of the rendered text starting attoffsetwith the same number of units ofotherstarting atooffset. WhenignoreCaseis true, each unit is folded withCharacter.toUpperCase(char)and, when those differ,Character.toLowerCase(char); otherwise the units are compared unchanged. A negative offset, or a region that does not fit in either text, yieldsfalse. A negativelencompares no units and yieldstruewhen both offsets are non-negative.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifotheris null.- Parameters:
ignoreCase-trueto fold case before comparing unitstoffset- first rendered index of the regionother- string that supplies the region to compare againstooffset- first index of the region inotherlen- number of UTF-16 code units to compare- Returns:
truewhen the regions match under that comparison, or whenlenis negative and both offsets are non-negative
-
startsWith
Reports whetherprefixoccurs in the rendered text at UTF-16 indextoffset. An empty prefix matches whentoffsetlies between zero and the rendered length, inclusive. A negative offset, or a prefix that would extend past the end, yieldsfalse.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifprefixis null.- Parameters:
prefix- sequence that must occur attoffsettoffset- UTF-16 index at whichprefixmust start- Returns:
truewhen the rendered text hasprefixthere
-
startsWith
Reports whether the rendered text begins withprefix. An empty prefix matches. The comparison starts at the first code unit.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifprefixis null.- Parameters:
prefix- sequence the rendered text must begin with- Returns:
truewhenprefixis a prefix of the rendered text
-
endsWith
Reports whether the rendered text ends withsuffix. An empty suffix matches.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifsuffixis null.- Parameters:
suffix- sequence the rendered text must end with- Returns:
truewhensuffixis a suffix of the rendered text
-
indexOf
public int indexOf(int ch) Returns the UTF-16 index of the first occurrence of code pointchin the rendered text, or-1when it does not occur. A basic-plane value matches one code unit, a supplementary code point matches its surrogate pair, and any otherintdoes not occur. The search starts at the first code unit.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Parameters:
ch- code point to search for- Returns:
- the index of the first match, or
-1when there is none
-
indexOf
public int indexOf(int ch, int fromIndex) Returns the UTF-16 index of the first occurrence of code pointchat or afterfromIndex, or-1when it does not occur. A negativefromIndexstarts at the beginning, and afromIndexat or beyond the rendered length yields-1. A basic-plane value matches one code unit, a supplementary code point matches its surrogate pair, and any otherintdoes not occur.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Parameters:
ch- code point to search forfromIndex- UTF-16 index at which the search starts- Returns:
- the index of the first match at or after
fromIndex, or-1when there is none
-
lastIndexOf
public int lastIndexOf(int ch) Returns the UTF-16 index of the last occurrence of code pointchin the rendered text, or-1when it does not occur. A basic-plane value matches one code unit, a supplementary code point matches its surrogate pair, and any otherintdoes not occur. The search starts at the last code unit.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Parameters:
ch- code point to search for- Returns:
- the index of the last match, or
-1when there is none
-
lastIndexOf
public int lastIndexOf(int ch, int fromIndex) Returns the UTF-16 index of the last occurrence of code pointchat or beforefromIndex, or-1when it does not occur. A negativefromIndexyields-1. AfromIndexpast the end searches the whole rendered text. A basic-plane value matches one code unit, a supplementary code point matches its surrogate pair, and any otherintdoes not occur.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Parameters:
ch- code point to search forfromIndex- UTF-16 index at which the backward search starts- Returns:
- the index of the last match at or before
fromIndex, or-1when there is none
-
indexOf
Returns the UTF-16 index of the first occurrence ofstrin the rendered text, or-1when it does not occur. The empty string occurs at index zero.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifstris null.- Parameters:
str- exact sequence to search for- Returns:
- the index of the first match, or
-1when there is none
-
indexOf
Returns the UTF-16 index of the first occurrence ofstrat or afterfromIndex, or-1when it does not occur. A negativefromIndexstarts at the beginning. The empty string occurs atfromIndexclamped into the range from zero through the rendered length.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifstris null.- Parameters:
str- exact sequence to search forfromIndex- UTF-16 index at which the search starts- Returns:
- the index of the first match at or after
fromIndex, or-1when there is none
-
lastIndexOf
Returns the UTF-16 index of the last occurrence ofstrin the rendered text, or-1when it does not occur. The empty string occurs at the rendered length.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifstris null.- Parameters:
str- exact sequence to search for- Returns:
- the index of the last match, or
-1when there is none
-
lastIndexOf
Returns the UTF-16 index of the last occurrence ofstrat or beforefromIndex, or-1when it does not occur. A negativefromIndexyields-1, and afromIndexpast the end searches the whole rendered text. The empty string occurs atfromIndexwhen that index is in range, and at the rendered length whenfromIndexis past the end.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifstris null.- Parameters:
str- exact sequence to search forfromIndex- UTF-16 index at which the backward search starts- Returns:
- the index of the last match at or before
fromIndex, or-1when there is none
-
substring
Returns the tail of the rendered text frombeginIndexinclusive through the last code unit. An index of zero returns the rendered string itself, and an index equal to the length returns an empty string.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsStringIndexOutOfBoundsExceptionifbeginIndexis negative or greater than the rendered length.- Parameters:
beginIndex- first rendered index to keep, inclusive- Returns:
- the tail of the rendered text, or the rendered string when
beginIndexis zero
-
substring
Returns the slice of the rendered text frombeginIndexinclusive toendIndexexclusive. A range that covers the whole rendering returns that rendered string itself.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsStringIndexOutOfBoundsExceptionifbeginIndexis negative,endIndexis greater than the rendered length, orbeginIndexis greater thanendIndex.- Parameters:
beginIndex- first rendered index to keep, inclusiveendIndex- rendered index at which the slice stops, exclusive- Returns:
- the requested slice, or the rendered string when the range covers the whole text
-
concat
Returns the rendered text followed bystr. An emptystrreturns the rendered string itself.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifstris null.- Parameters:
str- text appended after the rendered text- Returns:
- the rendered text with
strappended, or the rendered string whenstris empty
-
replace
Returns the rendered text with everyoldCharreplaced bynewChar. WhenoldChardoes not occur, or the two characters are equal, the rendered string is returned unchanged.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Parameters:
oldChar- code unit to replacenewChar- code unit written in its place- Returns:
- the rendered text with those replacements, or the rendered string when no code unit changes
-
matches
Reports whetherregexmatches the whole rendered text. A pattern that matches only a substring yieldsfalse. The pattern is compiled for this call.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifregexis null. ThrowsPatternSyntaxExceptionifregexcannot be compiled.- Parameters:
regex- regular expression matched against the entire rendering- Returns:
truewhen the pattern matches the whole rendered text
-
contains
Reports whethersoccurs in the rendered text. An empty sequence occurs at the start.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifsis null.- Parameters:
s- sequence to search for- Returns:
truewhensoccurs in the rendered text
-
replaceFirst
Returns the rendered text with the first substring matched byregexreplaced byreplacement. A dollar sign or backslash inreplacementis a matcher substitution, not a literal character. A pattern that matches nothing returns the rendered string unchanged.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifregexorreplacementis null. ThrowsPatternSyntaxExceptionifregexcannot be compiled.- Parameters:
regex- regular expression whose first match is replacedreplacement- text written over that match, with matcher substitution of dollar signs and backslashes- Returns:
- the rendered text after that one replacement
-
replaceAll
Returns the rendered text with every substring matched byregexreplaced byreplacement. A dollar sign or backslash inreplacementis a matcher substitution, not a literal character. A pattern that matches nothing returns the rendered string unchanged.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifregexorreplacementis null. ThrowsPatternSyntaxExceptionifregexcannot be compiled.- Parameters:
regex- regular expression whose matches are replacedreplacement- text written over each match, with matcher substitution of dollar signs and backslashes- Returns:
- the rendered text after every replacement
-
replace
Returns the rendered text with every occurrence of the literal sequencetargetreplaced byreplacement. The search is not a regular expression, so a dollar sign inreplacementstays a dollar sign. An emptytargetis written between every code unit, including both ends. Atargetthat does not occur returns the rendered string unchanged.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptioniftargetorreplacementis null.- Parameters:
target- literal sequence to replacereplacement- literal sequence written in its place- Returns:
- the rendered text after those literal replacements
-
split
Splits the rendered text around matches ofregex. A positivelimitapplies the pattern at mostlimit - 1times and leaves the unmatched tail in the last element. A limit of zero applies the pattern as many times as possible and drops trailing empty strings. A negative limit applies the pattern as many times as possible and keeps trailing empty strings. When the pattern matches nothing, the result is a one-element array holding the rendered text.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifregexis null. ThrowsPatternSyntaxExceptionifregexcannot be compiled.- Parameters:
regex- regular expression that separates the rendered textlimit- maximum number of applications of the pattern, with the zero and negative cases above- Returns:
- the segments produced by that split
-
split
Splits the rendered text around matches ofregexand drops trailing empty strings. The split uses a limit of zero. When the pattern matches nothing, the result is a one-element array holding the rendered text.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptionifregexis null. ThrowsPatternSyntaxExceptionifregexcannot be compiled.- Parameters:
regex- regular expression that separates the rendered text- Returns:
- the segments, without trailing empty strings
-
toLowerCase
Returns the rendered text converted to lower case with the case-mapping rules oflocale. Those rules can change the length of the text.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptioniflocaleis null.- Parameters:
locale- locale whose case mappings are applied- Returns:
- the lower-case form of the rendered text
-
toLowerCase
Returns the rendered text converted to lower case with the case-mapping rules ofLocale.getDefault()at the time of the call. Those rules can change the length of the text.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Returns:
- the lower-case form of the rendered text
-
toUpperCase
Returns the rendered text converted to upper case with the case-mapping rules oflocale. Those rules can change the length of the text.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call. ThrowsNullPointerExceptioniflocaleis null.- Parameters:
locale- locale whose case mappings are applied- Returns:
- the upper-case form of the rendered text
-
toUpperCase
Returns the rendered text converted to upper case with the case-mapping rules ofLocale.getDefault()at the time of the call. Those rules can change the length of the text.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Returns:
- the upper-case form of the rendered text
-
toCharArray
public char[] toCharArray()Copies the UTF-16 code units of the rendered text into a new array. Later writes to that array do not change this GString or the rendered string.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Returns:
- a new array holding the rendered code units
-
intern
Returns the pooled instance of the rendered text. The rendering is installed in the pool when no equal string is already there, and an equal string that was already pooled is the object returned.The text comes from
toString(). When this instance may cache its rendering, the stored text is reused; otherwise the literal segments and values are interpolated on every call.- Returns:
- the pooled string equal to the rendered text
-
toString
Description copied from class:GString- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classGString
-