Groovy JDK

java.lang
Class String

Method Summary
Object asType(Class c)

Provides a method to perform custom 'dynamic' type conversion to the given class using the as operator.

Pattern bitwiseNegate()
Turns a String into a regular expression Pattern
String center(Number numberOfChars, String padding)
Center a String and pad it with the characters appended around it
String center(Number numberOfChars)
Center a String and pad it with spaces appended around it
boolean contains(String text)
Provide an implementation of contains() like Collection#contains to make Strings more polymorphic.
int count(String text)
Count the number of occurencies of a substring.
byte[] decodeBase64()
Decode the String from Base64 into a byte array.
String denormalize()
Return a String with lines (separated by LF, CR/LF, or CR) terminated by the platform specific line separator.
Object eachLine(Closure closure)
Iterates through this String line by line.
Object eachLine(int firstLine, Closure closure)
Iterates through this String line by line.
String eachMatch(String regex, Closure closure)
Process each regex group matched substring of the given string.
String eachMatch(Pattern pattern, Closure closure)
Process each regex group matched substring of the given pattern.
Process execute()
Executes the command specified by self as a command-line process.
Process execute(String[] envp, File dir)
Executes the command specified by self with environment defined by envp and under the working directory dir.
Process execute(List envp, File dir)
Executes the command specified by self with environment defined by envp and under the working directory dir.
String find(String regex)
Finds the first occurrence of a regular expression String within a String.
String find(Pattern pattern)
Finds the first occurrence of a compiled regular expression Pattern within a String.
String find(String regex, Closure closure)
Finds the first occurrence of a regular expression String within a String.
String find(Pattern pattern, Closure closure)
Finds the first occurrence of a compiled regular expression Pattern within a String.
List findAll(String regex)
Finds all occurrences of a regular expression string within a String.
List findAll(Pattern pattern)
Finds all occurrences of a regular expression Pattern within a String.
List findAll(String regex, Closure closure)
Finds all occurrences of a regular expression string within a String.
List findAll(Pattern pattern, Closure closure)
Finds all occurrences of a compiled regular expression Pattern within a String.
String getAt(int index)
Support the subscript operator for String.
String getAt(IntRange range)
Support the range subscript operator for String with IntRange
String getAt(EmptyRange range)
Support the range subscript operator for String with EmptyRange
String getAt(Range range)
Support the range subscript operator for String
String getAt(Collection indices)
Select a List of characters from a String using a Collection to identify the indices to be selected.
char[] getChars()
Converts the given String into an array of characters.
boolean isBigDecimal()
Determine if a String can be parsed into a BigDecimal.
boolean isBigInteger()
Determine if a String can be parsed into a BigInteger.
boolean isCase(Object switchValue)
'Case' implementation for a String, which uses String#equals(Object) in order to allow Strings to be used in switch statements.
boolean isDouble()
Determine if a String can be parsed into a Double.
boolean isFloat()
Determine if a String can be parsed into a Float.
boolean isInteger()
Determine if a String can be parsed into an Integer.
boolean isLong()
Determine if a String can be parsed into a Long.
boolean isNumber()
Determine if a String can be parsed into a Number.
StringBuffer leftShift(Object value)
Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a String.
boolean matches(Pattern pattern)
Tells whether or not self matches the given compiled regular expression Pattern.
String minus(Object target)
Remove a part of a String.
String multiply(Number factor)
Repeat a String a certain number of times.
String next()
This method is called by the ++ operator for the class String.
String normalize()
Return a String with linefeeds and carriage returns normalized to linefeeds.
String padLeft(Number numberOfChars, String padding)
Pad a String with the characters appended to the left
String padLeft(Number numberOfChars)
Pad a String with the spaces appended to the left
String padRight(Number numberOfChars, String padding)
Pad a String with the characters appended to the right
String padRight(Number numberOfChars)
Pad a String with the spaces appended to the right
String plus(Object value)
Appends the String representation of the given operand to this string.
String previous()
This method is called by the -- operator for the class String.
List readLines()
Return the lines of a String as a List of Strings.
String replaceAll(Pattern pattern, String replacement)
Replaces all substrings of a String that match the given compiled regular expression with the given replacement.
String replaceAll(String regex, Closure closure)
Replaces all occurrences of a captured group by the result of a closure on that text.
String replaceAll(Pattern pattern, Closure closure)
Replaces all occurrences of a captured group by the result of a closure on that text.
String replaceFirst(Pattern pattern, String replacement)
Replaces the first substring of a String that matches the given compiled regular expression with the given replacement.
String reverse()
Creates a new string which is the reverse (backwards) of this string
int size()
Provide the standard Groovy size() method for String.
String[] split()
Convenience method to split a string (with whitespace as delimiter) Like tokenize, but returns an Array of Strings instead of a List
Object splitEachLine(String regex, Closure closure)
Iterates through the given String line by line, splitting each line using the given separator.
Object splitEachLine(Pattern pattern, Closure closure)
Iterates through the given String line by line, splitting each line using the given separator Pattern.
BigDecimal toBigDecimal()
Parse a String into a BigDecimal
BigInteger toBigInteger()
Parse a String into a BigInteger
Boolean toBoolean()
Converts the given string into a Boolean object.
Character toCharacter()
Converts the given string into a Character object using the first character in the string.
Double toDouble()
Parse a String into a Double
Float toFloat()
Parse a String into a Float
Integer toInteger()
Parse a String into an Integer
List toList()
Converts the given String into a List of strings of one character.
Long toLong()
Parse a String into a Long
Short toShort()
Parse a String into a Short
URI toURI()
Transforms a String representing a URI into a URI object.
URL toURL()
Transforms a String representing a URL into a URL object.
List tokenize(String token)
Tokenize a String based on the given string delimiter.
List tokenize(Character token)
Tokenize a String based on the given character delimiter.
List tokenize()
Tokenize a String (with a whitespace as the delimiter).
 
Method Detail

asType

public Object asType(Class c)
 

Provides a method to perform custom 'dynamic' type conversion to the given class using the as operator.

Example: '123' as Double

By default, the following types are supported:

If any other type is given, the call is delegated to Object#asType.
Parameters:
c - the desired class.
Returns:
the converted object
Since:
1.0

bitwiseNegate

public Pattern bitwiseNegate()
 
Turns a String into a regular expression Pattern
Returns:
the regular expression pattern
Since:
1.5.0

center

public String center(Number numberOfChars, String padding)
 
Center a String and pad it with the characters appended around it
Parameters:
numberOfChars - the total number of characters.
padding - the charaters used for padding.
Returns:
the String centered with padded character around
Since:
1.0

center

public String center(Number numberOfChars)
 
Center a String and pad it with spaces appended around it
Parameters:
numberOfChars - the total number of characters.
Returns:
the String centered with padded character around
Since:
1.0

contains

public boolean contains(String text)
 
Provide an implementation of contains() like Collection#contains to make Strings more polymorphic. This method is not required on JDK 1.5 onwards
Parameters:
text - a String to look for.
Returns:
true if this string contains the given text
Since:
1.0

count

public int count(String text)
 
Count the number of occurencies of a substring.
Parameters:
text - a substring.
Returns:
the number of occurrencies of the given string inside this String
Since:
1.0

decodeBase64

public byte[] decodeBase64()
 
Decode the String from Base64 into a byte array.
Returns:
the decoded bytes as an array
Since:
1.0

denormalize

public String denormalize()
 
Return a String with lines (separated by LF, CR/LF, or CR) terminated by the platform specific line separator.
Returns:
the denormalized string
Since:
1.6.0

eachLine

public Object eachLine(Closure closure)
 
Iterates through this String line by line. Each line is passed to the given 1 or 2 arg closure. If a 2 arg closure is found the line count is passed as the second argument.
Parameters:
closure - a closure.
Returns:
the last value returned by the closure
Since:
1.5.5
See:
String#eachLine.

eachLine

public Object eachLine(int firstLine, Closure closure)
 
Iterates through this String line by line. Each line is passed to the given 1 or 2 arg closure. If a 2 arg closure is found the line count is passed as the second argument.
Parameters:
firstLine - the line number value used for the first line (default is 1, set to 0 to start counting from 0).
closure - a closure (arg 1 is line, optional arg 2 is line number).
Returns:
the last value returned by the closure
Since:
1.5.7

eachMatch

public String eachMatch(String regex, Closure closure)
 
Process each regex group matched substring of the given string. If the closure parameter takes one argument, an array with all match groups is passed to it. If the closure takes as many arguments as there are match groups, then each parameter will be one match group.
Parameters:
regex - a Regex string.
closure - a closure with one parameter or as much parameters as groups.
Returns:
the source string
Since:
1.6.0

eachMatch

public String eachMatch(Pattern pattern, Closure closure)
 
Process each regex group matched substring of the given pattern. If the closure parameter takes one argument, an array with all match groups is passed to it. If the closure takes as many arguments as there are match groups, then each parameter will be one match group.
Parameters:
pattern - a regex Pattern.
closure - a closure with one parameter or as much parameters as groups.
Returns:
the source string
Since:
1.6.1

execute

public Process execute()
 
Executes the command specified by self as a command-line process.

For more control over Process construction you can use java.lang.ProcessBuilder (JDK 1.5+).

Returns:
the Process which has just started for this command line representation
Since:
1.0

execute

public Process execute(String[] envp, File dir)
 
Executes the command specified by self with environment defined by envp and under the working directory dir.

For more control over Process construction you can use java.lang.ProcessBuilder (JDK 1.5+).

Parameters:
envp - an array of Strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process..
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process..
Returns:
the Process which has just started for this command line representation.
Since:
1.0

execute

public Process execute(List envp, File dir)
 
Executes the command specified by self with environment defined by envp and under the working directory dir.

For more control over Process construction you can use java.lang.ProcessBuilder (JDK 1.5+).

Parameters:
envp - a List of Objects (converted to Strings using toString), each member of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process..
dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process..
Returns:
the Process which has just started for this command line representation.
Since:
1.0

find

public String find(String regex)
 
Finds the first occurrence of a regular expression String within a String. If the regex doesn't match, null will be returned.

For example, if the regex doesn't match the result is null:

    assert null == "New York, NY".find(/\d{5}/)

If it does match, we get the matching string back:

     assert "10292" == "New York, NY 10292-0098".find(/\d{5}/)

If we have capture groups in our expression, we still get back the full match

     assert "10292-0098" == "New York, NY 10292-0098".find(/(\d{5})-?(\d{4})/)

Parameters:
regex - the capturing regex.
Returns:
a String containing the matched portion, or null if the regex doesn't match
Since:
1.6.1

find

public String find(Pattern pattern)
 
Finds the first occurrence of a compiled regular expression Pattern within a String. If the pattern doesn't match, null will be returned.

For example, if the pattern doesn't match the result is null:

    assert null == "New York, NY".find(~/\d{5}/)

If it does match, we get the matching string back:

     assert "10292" == "New York, NY 10292-0098".find(~/\d{5}/)

If we have capture groups in our expression, the groups are ignored and we get back the full match:

     assert "10292-0098" == "New York, NY 10292-0098".find(~/(\d{5})-?(\d{4})/)
If you need to work with capture groups, then use the closure version of this method or use Groovy's matcher operators or use eachMatch.

Parameters:
pattern - the compiled regex Pattern.
Returns:
a String containing the matched portion, or null if the regex pattern doesn't match
Since:
1.6.1

find

public String find(String regex, Closure closure)
 
Finds the first occurrence of a regular expression String within a String. If the regex doesn't match, the closure will not be called and find will return null.

For example, if the regex doesn't match, the result is null:

    assert null == "New York, NY".find(~/\d{5}/) { match -> return "-$match-"}

If it does match and we don't have any capture groups in our regex, there is a single parameter on the closure that the match gets passed to:

     assert "-10292-" == "New York, NY 10292-0098".find(~/\d{5}/) { match -> return "-$match-"}

If we have capture groups in our expression, our closure has one parameter for the match, followed by one for each of the capture groups:

     assert "10292" == "New York, NY 10292-0098".find(~/(\d{5})-?(\d{4})/) { match, zip, plusFour ->
         assert match == "10292-0098"
         assert zip == "10292"
         assert plusFour == "0098"
         return zip
     }

If we have capture groups in our expression, and our closure has one parameter, the closure will be passed an array with the first element corresponding to the whole match, followed by an element for each of the capture groups:

     assert "10292" == "New York, NY 10292-0098".find(~/(\d{5})-?(\d{4})/) { match, zip, plusFour ->
         assert array[0] == "10292-0098"
         assert array[1] == "10292"
         assert array[2] == "0098"
         return array[1]
     }

If a capture group is optional, and doesn't match, then the corresponding value for that capture group passed to the closure will be null as illustrated here:

     assert "2339999" == "adsf 233-9999 adsf".find(~/(\d{3})?-?(\d{3})-(\d{4})/) { match, areaCode, exchange, stationNumber ->
         assert "233-9999" == match
         assert null == areaCode
         assert "233" == exchange
         assert "9999" == stationNumber
         return "$exchange$stationNumber"
     }

Parameters:
regex - the capturing regex string.
closure - the closure that will be passed the full match, plus each of the capturing groups.
Returns:
a String containing the result of the closure, or null if the regex pattern doesn't match
Since:
1.6.1

find

public String find(Pattern pattern, Closure closure)
 
Finds the first occurrence of a compiled regular expression Pattern within a String. If the pattern doesn't match, the closure will not be called and find will return null.

For example, if the pattern doesn't match, the result is null:

    assert null == "New York, NY".find(~/\d{5}/) { match -> return "-$match-"}

If it does match and we don't have any capture groups in our regex, there is a single parameter on the closure that the match gets passed to:

     assert "-10292-" == "New York, NY 10292-0098".find(~/\d{5}/) { match -> return "-$match-"}

If we have capture groups in our expression, our closure has one parameter for the match, followed by one for each of the capture groups:

     assert "10292" == "New York, NY 10292-0098".find(~/(\d{5})-?(\d{4})/) { match, zip, plusFour ->
         assert match == "10292-0098"
         assert zip == "10292"
         assert plusFour == "0098"
         return zip
     }

If we have capture groups in our expression, and our closure has one parameter, the closure will be passed an array with the first element corresponding to the whole match, followed by an element for each of the capture groups:

     assert "10292" == "New York, NY 10292-0098".find(~/(\d{5})-?(\d{4})/) { match, zip, plusFour ->
         assert array[0] == "10292-0098"
         assert array[1] == "10292"
         assert array[2] == "0098"
         return array[1]
     }

If a capture group is optional, and doesn't match, then the corresponding value for that capture group passed to the closure will be null as illustrated here:

     assert "2339999" == "adsf 233-9999 adsf".find(~/(\d{3})?-?(\d{3})-(\d{4})/) { match, areaCode, exchange, stationNumber ->
         assert "233-9999" == match
         assert null == areaCode
         assert "233" == exchange
         assert "9999" == stationNumber
         return "$exchange$stationNumber"
     }

Parameters:
pattern - the compiled regex Pattern.
closure - the closure that will be passed the full match, plus each of the capturing groups.
Returns:
a String containing the result of the closure, or null if the regex pattern doesn't match
Since:
1.6.1

findAll

public List findAll(String regex)
 
Finds all occurrences of a regular expression string within a String. A List is returned containing all full matches or an empty list if there are no matches within the string.

For example, if the regex doesn't match, it returns an empty list:

assert [] == "foo".findAll(/(\w*) Fish/)

Any regular expression matches are returned in a list, and all regex capture groupings are ignored, only the full match is returned:

def expected = ["One Fish", "Two Fish", "Red Fish", "Blue Fish"]
assert expected == "One Fish, Two Fish, Red Fish, Blue Fish".findAll(/(\w*) Fish/)
If you need to work with capture groups, then use the closure version of this method or use Groovy's matcher operators or use eachMatch.

Parameters:
regex - the capturing regex String.
Returns:
a List containing all full matches of the regex within the string, an empty list will be returned if there are no matches
Since:
1.6.1

findAll

public List findAll(Pattern pattern)
 
Finds all occurrences of a regular expression Pattern within a String. A List is returned containing all full matches or an empty list if there are no matches within the string.

For example, if the pattern doesn't match, it returns an empty list:

assert [] == "foo".findAll(~/(\w*) Fish/)

Any regular expression matches are returned in a list, and all regex capture groupings are ignored, only the full match is returned:

def expected = ["One Fish", "Two Fish", "Red Fish", "Blue Fish"]
assert expected == "One Fish, Two Fish, Red Fish, Blue Fish".findAll(~/(\w*) Fish/)
Parameters:
pattern - the compiled regex Pattern.
Returns:
a List containing all full matches of the Pattern within the string, an empty list will be returned if there are no matches
Since:
1.6.1

findAll

public List findAll(String regex, Closure closure)
 
Finds all occurrences of a regular expression string within a String. Any matches are passed to the specified closure. The closure is expected to have the full match in the first parameter. If there are any capture groups, they will be placed in subsequent parameters.

If there are no matches, the closure will not be called, and an empty List will be returned.

For example, if the regex doesn't match, it returns an empty list:

assert [] == "foo".findAll(/(\w*) Fish/) { match, firstWord -> return firstWord }

Any regular expression matches are passed to the closure, if there are no capture groups, there will be one parameter for the match:

assert ["couldn't", "wouldn't"] == "I could not, would not, with a fox.".findAll(/.ould/) { match -> "${match}n't"}

If there are capture groups, the first parameter will be the match followed by one parameter for each capture group:

def orig = "There's a Wocket in my Pocket"
assert ["W > Wocket", "P > Pocket"] == orig.findAll(/(.)ocket/) { match, firstLetter -> "$firstLetter > $match" }
Parameters:
regex - the capturing regex String.
closure - will be passed the full match plus each of the capturing groups.
Returns:
a List containing all full matches of the regex within the string, an empty list will be returned if there are no matches
Since:
1.6.1

findAll

public List findAll(Pattern pattern, Closure closure)
 
Finds all occurrences of a compiled regular expression Pattern within a String. Any matches are passed to the specified closure. The closure is expected to have the full match in the first parameter. If there are any capture groups, they will be placed in subsequent parameters.

If there are no matches, the closure will not be called, and an empty List will be returned.

For example, if the pattern doesn't match, it returns an empty list:

assert [] == "foo".findAll(~/(\w*) Fish/) { match, firstWord -> return firstWord }

Any regular expression matches are passed to the closure, if there are no capture groups, there will be one parameter for the match:

assert ["couldn't", "wouldn't"] == "I could not, would not, with a fox.".findAll(~/.ould/) { match -> "${match}n't"}

If there are capture groups, the first parameter will be the match followed by one parameter for each capture group:

def orig = "There's a Wocket in my Pocket"
assert ["W > Wocket", "P > Pocket"] == orig.findAll(~/(.)ocket/) { match, firstLetter -> "$firstLetter > $match" }
Parameters:
pattern - the compiled regex Pattern.
closure - will be passed the full match plus each of the capturing groups.
Returns:
a List containing all full matches of the regex Pattern within the string, an empty list will be returned if there are no matches
Since:
1.6.1

getAt

public String getAt(int index)
 
Support the subscript operator for String.
Parameters:
index - the index of the Character to get.
Returns:
the Character at the given index
Since:
1.0

getAt

public String getAt(IntRange range)
 
Support the range subscript operator for String with IntRange
Parameters:
range - an IntRange.
Returns:
the resulting String
Since:
1.0

getAt

public String getAt(EmptyRange range)
 
Support the range subscript operator for String with EmptyRange
Parameters:
range - an EmptyRange.
Returns:
the resulting String
Since:
1.5.0

getAt

public String getAt(Range range)
 
Support the range subscript operator for String
Parameters:
range - a Range.
Returns:
a substring corresponding to the Range
Since:
1.0

getAt

public String getAt(Collection indices)
 
Select a List of characters from a String using a Collection to identify the indices to be selected.
Parameters:
indices - a Collection of indices.
Returns:
a String consisting of the characters at the given indices
Since:
1.0

getChars

public char[] getChars()
 
Converts the given String into an array of characters. Alias for toCharArray.
Returns:
an array of characters
Since:
1.6.0
See:
String#toCharArray.

isBigDecimal

public boolean isBigDecimal()
 
Determine if a String can be parsed into a BigDecimal.
Returns:
true if the string can be parsed
Since:
1.5.0

isBigInteger

public boolean isBigInteger()
 
Determine if a String can be parsed into a BigInteger.
Returns:
true if the string can be parsed
Since:
1.5.0

isCase

public boolean isCase(Object switchValue)
 
'Case' implementation for a String, which uses String#equals(Object) in order to allow Strings to be used in switch statements. For example:
switch( str ) {
  case 'one' :
  // etc...
}
Note that this returns true for the case where both the 'switch' and 'case' operand is null.
Parameters:
switchValue - the switch value.
Returns:
true if the switchValue's toString() equals the caseValue
Since:
1.0

isDouble

public boolean isDouble()
 
Determine if a String can be parsed into a Double.
Returns:
true if the string can be parsed
Since:
1.5.0

isFloat

public boolean isFloat()
 
Determine if a String can be parsed into a Float.
Returns:
true if the string can be parsed
Since:
1.5.0

isInteger

public boolean isInteger()
 
Determine if a String can be parsed into an Integer.
Returns:
true if the string can be parsed
Since:
1.5.0

isLong

public boolean isLong()
 
Determine if a String can be parsed into a Long.
Returns:
true if the string can be parsed
Since:
1.5.0

isNumber

public boolean isNumber()
 
Determine if a String can be parsed into a Number. Synonym for 'isBigDecimal()'.
Returns:
true if the string can be parsed
Since:
1.5.0
See:
String#isBigDecimal.

leftShift

public StringBuffer leftShift(Object value)
 
Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a String.
Parameters:
value - an Obect.
Returns:
a StringBuffer built from this string
Since:
1.0

matches

public boolean matches(Pattern pattern)
 
Tells whether or not self matches the given compiled regular expression Pattern.
Parameters:
pattern - the regex Pattern to which the string of interest is to be matched.
Returns:
The resulting String
Since:
1.6.1
See:
String#matches.

minus

public String minus(Object target)
 
Remove a part of a String. This replaces the first occurrence of target within self with '' and returns the result. If target is a regex Pattern, the first occurrence of that pattern will be removed (using regex matching), otherwise the first occurrence of target.toString() will be removed.
Parameters:
target - an object representing the part to remove.
Returns:
a String minus the part to be removed
Since:
1.0

multiply

public String multiply(Number factor)
 
Repeat a String a certain number of times.
Parameters:
factor - the number of times the String should be repeated.
Returns:
a String composed of a repetition
Since:
1.0

next

public String next()
 
This method is called by the ++ operator for the class String. It increments the last character in the given string. If the character in the string is Character.MAX_VALUE a Character.MIN_VALUE will be appended. The empty string is incremented to a string consisting of the character Character.MIN_VALUE.
Returns:
an incremented String
Since:
1.0

normalize

public String normalize()
 
Return a String with linefeeds and carriage returns normalized to linefeeds.
Returns:
the normalized string
Since:
1.6.0

padLeft

public String padLeft(Number numberOfChars, String padding)
 
Pad a String with the characters appended to the left
Parameters:
numberOfChars - the total number of characters.
padding - the charaters used for padding.
Returns:
the String padded to the left
Since:
1.0

padLeft

public String padLeft(Number numberOfChars)
 
Pad a String with the spaces appended to the left
Parameters:
numberOfChars - the total number of characters.
Returns:
the String padded to the left
Since:
1.0

padRight

public String padRight(Number numberOfChars, String padding)
 
Pad a String with the characters appended to the right
Parameters:
numberOfChars - the total number of characters.
padding - the charaters used for padding.
Returns:
the String padded to the right
Since:
1.0

padRight

public String padRight(Number numberOfChars)
 
Pad a String with the spaces appended to the right
Parameters:
numberOfChars - the total number of characters.
Returns:
the String padded to the right
Since:
1.0

plus

public String plus(Object value)
 
Appends the String representation of the given operand to this string.
Parameters:
value - any Object.
Returns:
the new string with the object appended
Since:
1.0

previous

public String previous()
 
This method is called by the -- operator for the class String. It decrements the last character in the given string. If the character in the string is Character.MIN_VALUE it will be deleted. The empty string can't be decremented.
Returns:
a String with a decremented digit at the end
Since:
1.0

readLines

public List readLines()
 
Return the lines of a String as a List of Strings.
Returns:
a list of lines
Since:
1.5.5

replaceAll

public String replaceAll(Pattern pattern, String replacement)
 
Replaces all substrings of a String that match the given compiled regular expression with the given replacement.

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher#replaceAll. Use Matcher#quoteReplacement to suppress the special meaning of these characters, if desired.

Parameters:
pattern - the regex Pattern to which the string of interest is to be matched.
replacement - the string to be substituted for the first match.
Returns:
The resulting String
Since:
1.6.1
See:
String#replaceAll.

replaceAll

public String replaceAll(String regex, Closure closure)
 
Replaces all occurrences of a captured group by the result of a closure on that text.

For examples,

    assert "FOOBAR-FOOBAR-" == "foobar-FooBar-".replaceAll("(([fF][oO]{2})[bB]ar)", { Object[] it -> it[0].toUpperCase() })

Here, it[0] is the global string of the matched group it[1] is the first string in the matched group it[2] is the second string in the matched group

assert "FOO-FOO-" == "foobar-FooBar-".replaceAll("(([fF][oO]{2})[bB]ar)", { x, y, z -> z.toUpperCase() })

Here, x is the global string of the matched group y is the first string in the matched group z is the second string in the matched group

Note that unlike String.replaceAll(String regex, String replacement), where the replacement string treats '$' and '\' specially (for group substitution), the result of the closure is converted to a string and that value is used literally for the replacement.

Parameters:
regex - the capturing regex.
closure - the closure to apply on each captured group.
Returns:
a String with replaced content
Since:
1.0
See:
Matcher#quoteReplacement.

replaceAll

public String replaceAll(Pattern pattern, Closure closure)
 
Replaces all occurrences of a captured group by the result of a closure on that text.

For examples,

    assert "FOOBAR-FOOBAR-" == "foobar-FooBar-".replaceAll(~"(([fF][oO]{2})[bB]ar)", { Object[] it -> it[0].toUpperCase() })

Here, it[0] is the global string of the matched group it[1] is the first string in the matched group it[2] is the second string in the matched group

assert "FOO-FOO-" == "foobar-FooBar-".replaceAll("(([fF][oO]{2})[bB]ar)", { x, y, z -> z.toUpperCase() })

Here, x is the global string of the matched group y is the first string in the matched group z is the second string in the matched group

Note that unlike String.replaceAll(String regex, String replacement), where the replacement string treats '$' and '\' specially (for group substitution), the result of the closure is converted to a string and that value is used literally for the replacement.

Parameters:
pattern - the capturing regex Pattern.
closure - the closure to apply on each captured group.
Returns:
a String with replaced content
Since:
1.6.8
See:
Matcher#quoteReplacement.

replaceFirst

public String replaceFirst(Pattern pattern, String replacement)
 
Replaces the first substring of a String that matches the given compiled regular expression with the given replacement.

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher#replaceFirst. Use Matcher#quoteReplacement to suppress the special meaning of these characters, if desired.

Parameters:
pattern - the regex Pattern to which the string of interest is to be matched.
replacement - the string to be substituted for the first match.
Returns:
The resulting String
Since:
1.6.1
See:
String#replaceFirst.

reverse

public String reverse()
 
Creates a new string which is the reverse (backwards) of this string
Returns:
a new string with all the characters reversed.
Since:
1.0
See:
StringBuilder#reverse.

size

public int size()
 
Provide the standard Groovy size() method for String.
Returns:
the length of the String
Since:
1.0

split

public String[] split()
 
Convenience method to split a string (with whitespace as delimiter) Like tokenize, but returns an Array of Strings instead of a List
Returns:
String[] result of split
Since:
1.5.0

splitEachLine

public Object splitEachLine(String regex, Closure closure)
 
Iterates through the given String line by line, splitting each line using the given separator. The list of tokens for each line is then passed to the given closure.
Parameters:
regex - the delimiting regular expression.
closure - a closure.
Returns:
the last value returned by the closure
Since:
1.5.5
See:
String#split.

splitEachLine

public Object splitEachLine(Pattern pattern, Closure closure)
 
Iterates through the given String line by line, splitting each line using the given separator Pattern. The list of tokens for each line is then passed to the given closure.
Parameters:
pattern - the regular expression Pattern for the delimiter.
closure - a closure.
Returns:
the last value returned by the closure
Since:
1.6.8
See:
Pattern#split.

toBigDecimal

public BigDecimal toBigDecimal()
 
Parse a String into a BigDecimal
Returns:
a BigDecimal
Since:
1.0

toBigInteger

public BigInteger toBigInteger()
 
Parse a String into a BigInteger
Returns:
a BigInteger
Since:
1.0

toBoolean

public Boolean toBoolean()
 
Converts the given string into a Boolean object. If the trimmed string is "true", "y" or "1" (ignoring case) then the result is true othewrwise it is false.
Returns:
The Boolean value
Since:
1.0

toCharacter

public Character toCharacter()
 
Converts the given string into a Character object using the first character in the string.
Returns:
the first Character
Since:
1.0

toDouble

public Double toDouble()
 
Parse a String into a Double
Returns:
a Double
Since:
1.0

toFloat

public Float toFloat()
 
Parse a String into a Float
Returns:
a Float
Since:
1.0

toInteger

public Integer toInteger()
 
Parse a String into an Integer
Returns:
an Integer
Since:
1.0

toList

public List toList()
 
Converts the given String into a List of strings of one character.
Returns:
a List of characters (a 1-character String)
Since:
1.0

toLong

public Long toLong()
 
Parse a String into a Long
Returns:
a Long
Since:
1.0

toShort

public Short toShort()
 
Parse a String into a Short
Returns:
a Short
Since:
1.5.7

toURI

public URI toURI()
 
Transforms a String representing a URI into a URI object.
Returns:
a URI
Since:
1.0

toURL

public URL toURL()
 
Transforms a String representing a URL into a URL object.
Returns:
a URL
Since:
1.0

tokenize

public List tokenize(String token)
 
Tokenize a String based on the given string delimiter.
Parameters:
token - the delimiter.
Returns:
a List of tokens
Since:
1.0
See:
StringTokenizer#StringTokenizer.

tokenize

public List tokenize(Character token)
 
Tokenize a String based on the given character delimiter. For example:
char pathSep = ':'
assert "/tmp:/usr".tokenize(pathSep) == ["/tmp", "/usr"]
Parameters:
token - the delimiter.
Returns:
a List of tokens
Since:
1.7.2
See:
StringTokenizer#StringTokenizer.

tokenize

public List tokenize()
 
Tokenize a String (with a whitespace as the delimiter).
Returns:
a List of tokens
Since:
1.0
See:
StringTokenizer#StringTokenizer.

Groovy JDK