|
Groovy JDK |
Method Summary | |
---|---|
Object
|
asType(Class)
Provides a method to perform custom 'dynamic' type conversion
to the given class using the |
Pattern
|
bitwiseNegate()
Turns a String into a regular expression Pattern |
String
|
center(Number, String)
Center a String and pad it with the characters appended around it |
String
|
center(Number)
Center a String and pad it with spaces appended around it |
boolean
|
contains(String)
Provide an implementation of contains() like Collection#contains to make Strings more polymorphic. |
int
|
count(String)
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)
Iterates through this String line by line. |
Object
|
eachLine(int, Closure)
Iterates through this String line by line. |
String
|
eachMatch(String, Closure)
Process each regex group matched substring of the given string. |
String
|
eachMatch(Pattern, 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[], File)
Executes the command specified by self with environment defined by envp
and under the working directory dir .
|
Process
|
execute(List, File)
Executes the command specified by self with environment defined
by envp and under the working directory dir .
|
String
|
find(String)
Finds the first occurrence of a regular expression String within a String. |
String
|
find(Pattern)
Finds the first occurrence of a compiled regular expression Pattern within a String. |
String
|
find(String, Closure)
Finds the first occurrence of a regular expression String within a String. |
String
|
find(Pattern, Closure)
Finds the first occurrence of a compiled regular expression Pattern within a String. |
List
|
findAll(String)
Finds all occurrences of a regular expression string within a String. |
List
|
findAll(Pattern)
Finds all occurrences of a regular expression Pattern within a String. |
List
|
findAll(String, Closure)
Finds all occurrences of a regular expression string within a String. |
List
|
findAll(Pattern, Closure)
Finds all occurrences of a compiled regular expression Pattern within a String. |
String
|
getAt(int)
Support the subscript operator for String. |
String
|
getAt(IntRange)
Support the range subscript operator for String with IntRange |
String
|
getAt(EmptyRange)
Support the range subscript operator for String with EmptyRange |
String
|
getAt(Range)
Support the range subscript operator for String |
String
|
getAt(Collection)
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)
'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)
Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a String. |
boolean
|
matches(Pattern)
Tells whether or not self matches the given compiled regular expression Pattern. |
String
|
minus(Object)
Remove a part of a String. |
String
|
multiply(Number)
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, String)
Pad a String with the characters appended to the left |
String
|
padLeft(Number)
Pad a String with the spaces appended to the left |
String
|
padRight(Number, String)
Pad a String with the characters appended to the right |
String
|
padRight(Number)
Pad a String with the spaces appended to the right |
String
|
plus(Object)
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, String)
Replaces all substrings of a String that match the given compiled regular expression with the given replacement. |
String
|
replaceAll(String, Closure)
Replaces all occurrences of a captured group by the result of a closure on that text. |
String
|
replaceAll(Pattern, Closure)
Replaces all occurrences of a captured group by the result of a closure on that text. |
String
|
replaceFirst(Pattern, String)
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, Closure)
Iterates through the given String line by line, splitting each line using the given separator. |
Object
|
splitEachLine(Pattern, 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)
Tokenize a String based on the given string delimiter. |
List
|
tokenize()
Tokenize a String (with a whitespace as the delimiter). |
Method Detail |
---|
public Object asType(Class)
Provides a method to perform custom 'dynamic' type conversion
to the given class using the as
operator.
'123' as Double
By default, the following types are supported:
c
- the desired class.public Pattern bitwiseNegate()
public String center(Number, String)
numberOfChars
- the total number of characters.padding
- the charaters used for padding.public String center(Number)
numberOfChars
- the total number of characters.public boolean contains(String)
text
- a String to look for.public int count(String)
text
- a substring.public byte[] decodeBase64()
public String denormalize()
public Object eachLine(Closure)
closure
- a closure.public Object eachLine(int, Closure)
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).public String eachMatch(String, Closure)
regex
- a Regex string.closure
- a closure with one parameter or as much parameters as groups.public String eachMatch(Pattern, Closure)
pattern
- a regex Pattern.closure
- a closure with one parameter or as much parameters as groups.public Process execute()
self
as a command-line process.
For more control over Process construction you can use
java.lang.ProcessBuilder
(JDK 1.5+).
public Process execute(String[], File)
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+).
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..public Process execute(List, File)
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+).
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..public String find(String)
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})/)
regex
- the capturing regex.public String find(Pattern)
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.
pattern
- the compiled regex Pattern.public String find(String, Closure)
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" }
regex
- the capturing regex string.closure
- the closure that will be passed the full match, plus each of the capturing groups.public String find(Pattern, Closure)
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" }
pattern
- the compiled regex Pattern.closure
- the closure that will be passed the full match, plus each of the capturing groups.public List findAll(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.
regex
- the capturing regex String.public List findAll(Pattern)
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/)
pattern
- the compiled regex Pattern.public List findAll(String, Closure)
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" }
regex
- the capturing regex String.closure
- will be passed the full match plus each of the capturing groups.public List findAll(Pattern, Closure)
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" }
pattern
- the compiled regex Pattern.closure
- will be passed the full match plus each of the capturing groups.public String getAt(int)
index
- the index of the Character to get.public String getAt(IntRange)
range
- an IntRange.public String getAt(EmptyRange)
range
- an EmptyRange.public String getAt(Range)
range
- a Range.public String getAt(Collection)
indices
- a Collection of indices.public char[] getChars()
public boolean isBigDecimal()
public boolean isBigInteger()
public boolean isCase(Object)
switch( str ) { case 'one' : // etc... }Note that this returns
true
for the case where both the
'switch' and 'case' operand is null
.switchValue
- the switch value.public boolean isDouble()
public boolean isFloat()
public boolean isInteger()
public boolean isLong()
public boolean isNumber()
public StringBuffer leftShift(Object)
value
- an Obect.public boolean matches(Pattern)
pattern
- the regex Pattern to which the string of interest is to be matched.public String minus(Object)
target
- an object representing the part to remove.public String multiply(Number)
factor
- the number of times the String should be repeated.public String next()
public String normalize()
public String padLeft(Number, String)
numberOfChars
- the total number of characters.padding
- the charaters used for padding.public String padLeft(Number)
numberOfChars
- the total number of characters.public String padRight(Number, String)
numberOfChars
- the total number of characters.padding
- the charaters used for padding.public String padRight(Number)
numberOfChars
- the total number of characters.public String plus(Object)
value
- any Object.public String previous()
public List readLines()
public String replaceAll(Pattern, String)
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.
pattern
- the regex Pattern to which the string of interest is to be matched.replacement
- the string to be substituted for the first match.public String replaceAll(String, Closure)
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.
regex
- the capturing regex.closure
- the closure to apply on each captured group.public String replaceAll(Pattern, Closure)
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.
pattern
- the capturing regex Pattern.closure
- the closure to apply on each captured group.public String replaceFirst(Pattern, String)
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.
pattern
- the regex Pattern to which the string of interest is to be matched.replacement
- the string to be substituted for the first match.public String reverse()
public int size()
size()
method for String
.public String[] split()
public Object splitEachLine(String, Closure)
regex
- the delimiting regular expression.closure
- a closure.public Object splitEachLine(Pattern, Closure)
pattern
- the regular expression Pattern for the delimiter.closure
- a closure.public BigDecimal toBigDecimal()
public BigInteger toBigInteger()
public Boolean toBoolean()
public Character toCharacter()
public Double toDouble()
public Float toFloat()
public Integer toInteger()
public List toList()
public Long toLong()
public Short toShort()
public URI toURI()
public URL toURL()
public List tokenize(String)
token
- the delimiter.public List tokenize()
|
Groovy JDK |