public abstract class GString
extends GroovyObjectSupport
implements Buildable, Comparable, CharSequence, Writable, Serializable
Represents a String which contains embedded values such as "hello there ${user} how are you?" which can be evaluated lazily. Advanced users can iterate over the text and values to perform special processing, such as for performing SQL operations, the values can be substituted for ? and the actual value objects can be bound to a JDBC statement.
James Strachan: The lovely name of this class was suggested by Jules Gosnell and was such a good idea, I couldn't resist :)
| Modifiers | Name | Description |
|---|---|---|
static GString |
EMPTY |
A GString containing a single empty String and no values. |
static Object[] |
EMPTY_OBJECT_ARRAY |
Shared empty object array. |
static String[] |
EMPTY_STRING_ARRAY |
Shared empty string array. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
build(GroovyObject builder)Builds this object into the supplied builder.
|
|
protected int |
calcInitialCapacity()Calculates the initial buffer capacity for rendering this GString. |
|
public char |
charAt(int index){@inheritDoc} |
|
public int |
compareTo(Object that){@inheritDoc} |
|
public boolean |
equals(Object that){@inheritDoc} |
|
public boolean |
equals(GString that)Compares this GString with another GString by rendered content. |
|
public byte[] |
getBytes()Returns the rendered string as a byte array using the platform default charset. |
|
public byte[] |
getBytes(String charset)Returns the rendered string as a byte array using the supplied charset. |
|
public abstract String[] |
getStrings()Returns the string segments surrounding the interpolated values. |
|
public Object |
getValue(int idx)Returns the interpolated value at the supplied index. |
|
public int |
getValueCount()Returns the number of interpolated values. |
|
public Object[] |
getValues()Returns the interpolated values. |
|
public int |
hashCode(){@inheritDoc} |
|
public Object |
invokeMethod(String name, Object args)Overloaded to implement duck typing for Strings so that any method that can't be evaluated on this object will be forwarded to the toString() object instead. |
|
public int |
length(){@inheritDoc} |
|
public Pattern |
negate()Turns a String into a regular expression pattern |
|
public GString |
plus(GString that)Concatenates this GString with another GString. |
|
public GString |
plus(String that)Concatenates this GString with a String. |
|
public CharSequence |
subSequence(int start, int end){@inheritDoc} |
|
public String |
toString(){@inheritDoc} |
|
public Writer |
writeTo(Writer out)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.)
|
| Methods inherited from class | Name |
|---|---|
class GroovyObjectSupport |
getMetaClass, setMetaClass |
A GString containing a single empty String and no values.
Shared empty object array.
Shared empty string array.
Creates a GString from the supplied values array reference.
values - the interpolated valuesCreates a GString from the supplied values.
values - the interpolated valuesBuilds this object into the supplied builder.
builder - the target builderCalculates the initial buffer capacity for rendering this GString.
{@inheritDoc}
{@inheritDoc}
{@inheritDoc}
Compares this GString with another GString by rendered content.
that - the other GStringtrue if the rendered strings are equalReturns the rendered string as a byte array using the platform default charset.
Returns the rendered string as a byte array using the supplied charset.
charset - the charset nameReturns the string segments surrounding the interpolated values.
Returns the interpolated value at the supplied index.
idx - the value indexReturns the number of interpolated values.
Returns the interpolated values.
{@inheritDoc}
Overloaded to implement duck typing for Strings so that any method that can't be evaluated on this object will be forwarded to the toString() object instead.
{@inheritDoc}
Turns a String into a regular expression pattern
Concatenates this GString with another GString.
that - the other GStringConcatenates this GString with a String.
that - the string to append{@inheritDoc}
{@inheritDoc}
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.)
out - the Writer to which this Writable should output its data.Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.