Class GStringImpl

All Implemented Interfaces:
Buildable, GroovyObject, Writable, Serializable, CharSequence, Comparable

public class GStringImpl extends GString
Default implementation of a GString used by the compiler. A GString consists of a list of values and strings which can be combined to create a new String.
See Also:
  • Field Summary

    Fields inherited from class groovy.lang.GString

    EMPTY, EMPTY_OBJECT_ARRAY, EMPTY_STRING_ARRAY
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    GStringImpl(Object[] values, String[] strings)
    Create a new GString with values and strings.
    protected
    GStringImpl(Object[] values, String[] strings, boolean cacheable, String cachedStringLiteral, boolean frozen)
    Create a new GString with values and strings.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Builds this object into the supplied builder.
    protected int
    Calculates the initial buffer capacity for rendering this GString.
    int
    codePointAt(int index)
    Returns the Unicode code point at UTF-16 index index in the rendered text.
    int
    codePointBefore(int index)
    Returns the Unicode code point that ends just before UTF-16 index index.
    int
    codePointCount(int beginIndex, int endIndex)
    Returns how many Unicode code points lie in the rendered text from beginIndex inclusive to endIndex exclusive.
    int
    compareTo(String anotherString)
    Orders the rendered text against anotherString by UTF-16 code unit, using the same ordering as String.compareTo(String).
    int
    Orders the rendered text against str by UTF-16 code unit after case folding.
    Returns the rendered text followed by str.
    boolean
    Reports whether s occurs in the rendered text.
    boolean
    Reports whether the rendered text and cs have the same length and the same characters.
    boolean
    Reports whether the rendered text and sb have the same length and the same characters.
    boolean
    endsWith(String suffix)
    Reports whether the rendered text ends with suffix.
    boolean
    equalsIgnoreCase(String anotherString)
    Reports whether the rendered text and anotherString are equal ignoring case.
     
    byte[]
    getBytes(Charset charset)
    Encodes the rendered text with charset into a new byte array.
    void
    getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
    Copies UTF-16 code units from the rendered text into dst.
    Get the strings of this GString.
    Returns the interpolated values.
    int
    indexOf(int ch)
    Returns the UTF-16 index of the first occurrence of code point ch in the rendered text, or -1 when it does not occur.
    int
    indexOf(int ch, int fromIndex)
    Returns the UTF-16 index of the first occurrence of code point ch at or after fromIndex, or -1 when it does not occur.
    int
    Returns the UTF-16 index of the first occurrence of str in the rendered text, or -1 when it does not occur.
    int
    indexOf(String str, int fromIndex)
    Returns the UTF-16 index of the first occurrence of str at or after fromIndex, or -1 when it does not occur.
    Returns the pooled instance of the rendered text.
    boolean
    Returns true if the string is empty or contains only white space codepoints, otherwise false.
    boolean
    Reports whether the rendered text contains no code units.
    int
    lastIndexOf(int ch)
    Returns the UTF-16 index of the last occurrence of code point ch in the rendered text, or -1 when it does not occur.
    int
    lastIndexOf(int ch, int fromIndex)
    Returns the UTF-16 index of the last occurrence of code point ch at or before fromIndex, or -1 when it does not occur.
    int
    Returns the UTF-16 index of the last occurrence of str in the rendered text, or -1 when it does not occur.
    int
    lastIndexOf(String str, int fromIndex)
    Returns the UTF-16 index of the last occurrence of str at or before fromIndex, or -1 when it does not occur.
    Returns a stream of lines extracted from this string, separated by line terminators.
    boolean
    matches(String regex)
    Reports whether regex matches the whole rendered text.
    int
    offsetByCodePoints(int index, int codePointOffset)
    Returns the UTF-16 index reached by moving codePointOffset code points from index.
    plus(GString that)
    Concatenates this GString with another GString.
    boolean
    regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
    Compares len UTF-16 code units of the rendered text starting at toffset with the same number of units of other starting at ooffset.
    boolean
    regionMatches(int toffset, String other, int ooffset, int len)
    Compares len UTF-16 code units of the rendered text starting at toffset with len code units of other starting at ooffset.
    repeat(int count)
    Returns a string whose value is the concatenation of this string repeated count times.
    replace(char oldChar, char newChar)
    Returns the rendered text with every oldChar replaced by newChar.
    replace(CharSequence target, CharSequence replacement)
    Returns the rendered text with every occurrence of the literal sequence target replaced by replacement.
    replaceAll(String regex, String replacement)
    Returns the rendered text with every substring matched by regex replaced by replacement.
    replaceFirst(String regex, String replacement)
    Returns the rendered text with the first substring matched by regex replaced by replacement.
    split(String regex)
    Splits the rendered text around matches of regex and drops trailing empty strings.
    split(String regex, int limit)
    Splits the rendered text around matches of regex.
    boolean
    Reports whether the rendered text begins with prefix.
    boolean
    startsWith(String prefix, int toffset)
    Reports whether prefix occurs in the rendered text at UTF-16 index toffset.
    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 from beginIndex inclusive through the last code unit.
    substring(int beginIndex, int endIndex)
    Returns the slice of the rendered text from beginIndex inclusive to endIndex exclusive.
    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 of Locale.getDefault() at the time of the call.
    Returns the rendered text converted to lower case with the case-mapping rules of locale.
    Returns the rendered text converted to upper case with the case-mapping rules of Locale.getDefault() at the time of the call.
    Returns the rendered text converted to upper case with the case-mapping rules of locale.
    Returns the rendered text with every leading and trailing UTF-16 code unit whose value is at most U+0020 removed.
    Writes this object to the given writer.

    Methods inherited from class groovy.lang.GroovyObjectSupport

    getMetaClass, setMetaClass

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.CharSequence

    chars, codePoints

    Methods inherited from interface groovy.lang.GroovyObject

    getProperty, setProperty
  • Constructor Details

    • GStringImpl

      @GroovyABI(since="1.0.0") public GStringImpl(Object[] values, String[] strings)
      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 parts
      strings - 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 parts
      strings - the string parts
      frozen - creates a GStringImpl which is not subject to mutation and hence more amenable to caching
  • Method Details

    • plus

      public GString plus(GString that)
      Description copied from class: GString
      Concatenates this GString with another GString.
      Overrides:
      plus in class GString
      Parameters:
      that - the other GString
      Returns:
      the concatenated GString
    • writeTo

      public Writer writeTo(Writer out) throws IOException
      Description copied from class: GString
      Writes 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:
      writeTo in interface Writable
      Overrides:
      writeTo in class GString
      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

      public void build(GroovyObject builder)
      Description copied from class: GString
      Builds this object into the supplied builder.
      Specified by:
      build in interface Buildable
      Overrides:
      build in class GString
      Parameters:
      builder - the target builder
    • calcInitialCapacity

      protected int calcInitialCapacity()
      Description copied from class: GString
      Calculates the initial buffer capacity for rendering this GString.
      Overrides:
      calcInitialCapacity in class GString
      Returns:
      the initial capacity
    • freeze

      public GString freeze()
      Returns:
      returns an equivalent optimised but less mutable version of this GString
    • getStrings

      public String[] 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:
      getStrings in class GString
      Returns:
      the string segments
    • getValues

      public Object[] getValues()
      Description copied from class: GString
      Returns the interpolated values.
      Overrides:
      getValues in class GString
      Returns:
      the interpolated values
    • trim

      public String trim()
      Returns the rendered text with every leading and trailing UTF-16 code unit whose value is at most U+0020 removed. 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:
      true when the rendered length is zero
    • lines

      public Stream<String> 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 true if the string is empty or contains only white space codepoints, otherwise false.
      Returns:
      true if the string is empty or contains only white space codepoints, otherwise false
      Since:
      5.0.0
      See Also:
    • repeat

      public String repeat(int count)
      Returns a string whose value is the concatenation of this string repeated count times.

      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 count times or the empty string if this string is empty or count is zero
      Throws:
      IllegalArgumentException - if the count is negative.
      Since:
      5.0.0
    • stripLeading

      public String stripLeading()
      Returns a string whose value is this string, with all leading white space removed.

      If this String object 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

      public String stripTrailing()
      Returns a string whose value is this string, with all trailing white space removed.

      If this String object 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

      public String strip()
      Returns a string whose value is this string, with all leading and trailing white space removed.

      If this String object 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 index index in 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. Throws StringIndexOutOfBoundsException if index is 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 index index. When the code unit at index - 1 is 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 at index - 1 is 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. Throws StringIndexOutOfBoundsException if index is 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 from beginIndex inclusive to endIndex exclusive. 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. Throws IndexOutOfBoundsException if beginIndex is negative, endIndex is greater than the rendered length, or beginIndex is greater than endIndex.

      Parameters:
      beginIndex - first UTF-16 index of the range, inclusive
      endIndex - 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 moving codePointOffset code points from index. An unpaired surrogate counts as one code point. index may 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. Throws IndexOutOfBoundsException if index is 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 inclusive
      codePointOffset - 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 into dst. The copied range starts at srcBegin inclusive and stops at srcEnd exclusive, and the first copied unit is written at dstBegin.

      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. Throws NullPointerException if dst is null. Throws StringIndexOutOfBoundsException if the source range falls outside the rendered text, or if dst has no room for the copied units starting at dstBegin.

      Parameters:
      srcBegin - first rendered index to copy, inclusive
      srcEnd - rendered index at which copying stops, exclusive
      dst - array that receives the copied code units
      dstBegin - index in dst of the first copied unit
    • getBytes

      public byte[] getBytes(Charset charset)
      Encodes the rendered text with charset into 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. Throws NullPointerException if charset is null.

      Parameters:
      charset - encoding used to turn the rendered text into bytes
      Returns:
      a new array holding that encoding
    • contentEquals

      public boolean contentEquals(StringBuffer sb)
      Reports whether the rendered text and sb have 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. Throws NullPointerException if sb is null.

      Parameters:
      sb - buffer whose current contents are compared
      Returns:
      true when both sides have the same length and characters
    • contentEquals

      public boolean contentEquals(CharSequence cs)
      Reports whether the rendered text and cs have the same length and the same characters. A String is compared with String.equals(Object). A StringBuffer is locked for the read. A StringBuilder is compared without a lock. Any other sequence is walked with its length and charAt.

      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. Throws NullPointerException if cs is null.

      Parameters:
      cs - sequence compared with the rendered text
      Returns:
      true when both sides have the same length and characters
    • equalsIgnoreCase

      public boolean equalsIgnoreCase(String anotherString)
      Reports whether the rendered text and anotherString are equal ignoring case. The lengths must already match, and each UTF-16 code unit is then folded with Character.toUpperCase(char) and, when those differ, Character.toLowerCase(char). No locale is consulted, so the result can disagree with folding both sides through String.toLowerCase(). A null argument yields false.

      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:
      true when the two texts match under that case folding
    • compareTo

      public int compareTo(String anotherString)
      Orders the rendered text against anotherString by UTF-16 code unit, using the same ordering as String.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. Throws NullPointerException if anotherString is 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

      public int compareToIgnoreCase(String str)
      Orders the rendered text against str by UTF-16 code unit after case folding. Each unit is folded with Character.toUpperCase(char) and then Character.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. Throws NullPointerException if str is 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

      public boolean regionMatches(int toffset, String other, int ooffset, int len)
      Compares len UTF-16 code units of the rendered text starting at toffset with len code units of other starting at ooffset. A negative offset, or a region that does not fit in either text, yields false. A negative len compares no units and yields true when 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. Throws NullPointerException if other is null.

      Parameters:
      toffset - first rendered index of the region
      other - string that supplies the region to compare against
      ooffset - first index of the region in other
      len - number of UTF-16 code units to compare
      Returns:
      true when the compared units match, or when len is negative and both offsets are non-negative
    • regionMatches

      public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
      Compares len UTF-16 code units of the rendered text starting at toffset with the same number of units of other starting at ooffset. When ignoreCase is true, each unit is folded with Character.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, yields false. A negative len compares no units and yields true when 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. Throws NullPointerException if other is null.

      Parameters:
      ignoreCase - true to fold case before comparing units
      toffset - first rendered index of the region
      other - string that supplies the region to compare against
      ooffset - first index of the region in other
      len - number of UTF-16 code units to compare
      Returns:
      true when the regions match under that comparison, or when len is negative and both offsets are non-negative
    • startsWith

      public boolean startsWith(String prefix, int toffset)
      Reports whether prefix occurs in the rendered text at UTF-16 index toffset. An empty prefix matches when toffset lies between zero and the rendered length, inclusive. A negative offset, or a prefix that would extend past the end, yields false.

      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. Throws NullPointerException if prefix is null.

      Parameters:
      prefix - sequence that must occur at toffset
      toffset - UTF-16 index at which prefix must start
      Returns:
      true when the rendered text has prefix there
    • startsWith

      public boolean startsWith(String prefix)
      Reports whether the rendered text begins with prefix. 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. Throws NullPointerException if prefix is null.

      Parameters:
      prefix - sequence the rendered text must begin with
      Returns:
      true when prefix is a prefix of the rendered text
    • endsWith

      public boolean endsWith(String suffix)
      Reports whether the rendered text ends with suffix. 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. Throws NullPointerException if suffix is null.

      Parameters:
      suffix - sequence the rendered text must end with
      Returns:
      true when suffix is a suffix of the rendered text
    • indexOf

      public int indexOf(int ch)
      Returns the UTF-16 index of the first occurrence of code point ch in the rendered text, or -1 when it does not occur. A basic-plane value matches one code unit, a supplementary code point matches its surrogate pair, and any other int does 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 -1 when there is none
    • indexOf

      public int indexOf(int ch, int fromIndex)
      Returns the UTF-16 index of the first occurrence of code point ch at or after fromIndex, or -1 when it does not occur. A negative fromIndex starts at the beginning, and a fromIndex at 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 other int does 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 for
      fromIndex - UTF-16 index at which the search starts
      Returns:
      the index of the first match at or after fromIndex, or -1 when there is none
    • lastIndexOf

      public int lastIndexOf(int ch)
      Returns the UTF-16 index of the last occurrence of code point ch in the rendered text, or -1 when it does not occur. A basic-plane value matches one code unit, a supplementary code point matches its surrogate pair, and any other int does 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 -1 when there is none
    • lastIndexOf

      public int lastIndexOf(int ch, int fromIndex)
      Returns the UTF-16 index of the last occurrence of code point ch at or before fromIndex, or -1 when it does not occur. A negative fromIndex yields -1. A fromIndex past 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 other int does 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 for
      fromIndex - UTF-16 index at which the backward search starts
      Returns:
      the index of the last match at or before fromIndex, or -1 when there is none
    • indexOf

      public int indexOf(String str)
      Returns the UTF-16 index of the first occurrence of str in the rendered text, or -1 when 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. Throws NullPointerException if str is null.

      Parameters:
      str - exact sequence to search for
      Returns:
      the index of the first match, or -1 when there is none
    • indexOf

      public int indexOf(String str, int fromIndex)
      Returns the UTF-16 index of the first occurrence of str at or after fromIndex, or -1 when it does not occur. A negative fromIndex starts at the beginning. The empty string occurs at fromIndex clamped 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. Throws NullPointerException if str is null.

      Parameters:
      str - exact sequence to search for
      fromIndex - UTF-16 index at which the search starts
      Returns:
      the index of the first match at or after fromIndex, or -1 when there is none
    • lastIndexOf

      public int lastIndexOf(String str)
      Returns the UTF-16 index of the last occurrence of str in the rendered text, or -1 when 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. Throws NullPointerException if str is null.

      Parameters:
      str - exact sequence to search for
      Returns:
      the index of the last match, or -1 when there is none
    • lastIndexOf

      public int lastIndexOf(String str, int fromIndex)
      Returns the UTF-16 index of the last occurrence of str at or before fromIndex, or -1 when it does not occur. A negative fromIndex yields -1, and a fromIndex past the end searches the whole rendered text. The empty string occurs at fromIndex when that index is in range, and at the rendered length when fromIndex is 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. Throws NullPointerException if str is null.

      Parameters:
      str - exact sequence to search for
      fromIndex - UTF-16 index at which the backward search starts
      Returns:
      the index of the last match at or before fromIndex, or -1 when there is none
    • substring

      public String substring(int beginIndex)
      Returns the tail of the rendered text from beginIndex inclusive 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. Throws StringIndexOutOfBoundsException if beginIndex is 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 beginIndex is zero
    • substring

      public String substring(int beginIndex, int endIndex)
      Returns the slice of the rendered text from beginIndex inclusive to endIndex exclusive. 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. Throws StringIndexOutOfBoundsException if beginIndex is negative, endIndex is greater than the rendered length, or beginIndex is greater than endIndex.

      Parameters:
      beginIndex - first rendered index to keep, inclusive
      endIndex - rendered index at which the slice stops, exclusive
      Returns:
      the requested slice, or the rendered string when the range covers the whole text
    • concat

      public String concat(String str)
      Returns the rendered text followed by str. An empty str returns 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. Throws NullPointerException if str is null.

      Parameters:
      str - text appended after the rendered text
      Returns:
      the rendered text with str appended, or the rendered string when str is empty
    • replace

      public String replace(char oldChar, char newChar)
      Returns the rendered text with every oldChar replaced by newChar. When oldChar does 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 replace
      newChar - code unit written in its place
      Returns:
      the rendered text with those replacements, or the rendered string when no code unit changes
    • matches

      public boolean matches(String regex)
      Reports whether regex matches the whole rendered text. A pattern that matches only a substring yields false. 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. Throws NullPointerException if regex is null. Throws PatternSyntaxException if regex cannot be compiled.

      Parameters:
      regex - regular expression matched against the entire rendering
      Returns:
      true when the pattern matches the whole rendered text
    • contains

      public boolean contains(CharSequence s)
      Reports whether s occurs 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. Throws NullPointerException if s is null.

      Parameters:
      s - sequence to search for
      Returns:
      true when s occurs in the rendered text
    • replaceFirst

      public String replaceFirst(String regex, String replacement)
      Returns the rendered text with the first substring matched by regex replaced by replacement. A dollar sign or backslash in replacement is 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. Throws NullPointerException if regex or replacement is null. Throws PatternSyntaxException if regex cannot be compiled.

      Parameters:
      regex - regular expression whose first match is replaced
      replacement - text written over that match, with matcher substitution of dollar signs and backslashes
      Returns:
      the rendered text after that one replacement
    • replaceAll

      public String replaceAll(String regex, String replacement)
      Returns the rendered text with every substring matched by regex replaced by replacement. A dollar sign or backslash in replacement is 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. Throws NullPointerException if regex or replacement is null. Throws PatternSyntaxException if regex cannot be compiled.

      Parameters:
      regex - regular expression whose matches are replaced
      replacement - text written over each match, with matcher substitution of dollar signs and backslashes
      Returns:
      the rendered text after every replacement
    • replace

      public String replace(CharSequence target, CharSequence replacement)
      Returns the rendered text with every occurrence of the literal sequence target replaced by replacement. The search is not a regular expression, so a dollar sign in replacement stays a dollar sign. An empty target is written between every code unit, including both ends. A target that 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. Throws NullPointerException if target or replacement is null.

      Parameters:
      target - literal sequence to replace
      replacement - literal sequence written in its place
      Returns:
      the rendered text after those literal replacements
    • split

      public String[] split(String regex, int limit)
      Splits the rendered text around matches of regex. A positive limit applies the pattern at most limit - 1 times 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. Throws NullPointerException if regex is null. Throws PatternSyntaxException if regex cannot be compiled.

      Parameters:
      regex - regular expression that separates the rendered text
      limit - maximum number of applications of the pattern, with the zero and negative cases above
      Returns:
      the segments produced by that split
    • split

      public String[] split(String regex)
      Splits the rendered text around matches of regex and 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. Throws NullPointerException if regex is null. Throws PatternSyntaxException if regex cannot be compiled.

      Parameters:
      regex - regular expression that separates the rendered text
      Returns:
      the segments, without trailing empty strings
    • toLowerCase

      public String toLowerCase(Locale locale)
      Returns the rendered text converted to lower case with the case-mapping rules of locale. 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. Throws NullPointerException if locale is null.

      Parameters:
      locale - locale whose case mappings are applied
      Returns:
      the lower-case form of the rendered text
    • toLowerCase

      public String toLowerCase()
      Returns the rendered text converted to lower case with the case-mapping rules of Locale.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

      public String toUpperCase(Locale locale)
      Returns the rendered text converted to upper case with the case-mapping rules of locale. 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. Throws NullPointerException if locale is null.

      Parameters:
      locale - locale whose case mappings are applied
      Returns:
      the upper-case form of the rendered text
    • toUpperCase

      public String toUpperCase()
      Returns the rendered text converted to upper case with the case-mapping rules of Locale.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

      public String 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

      public String toString()
      Description copied from class: GString
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class GString