|
Groovy 2.2.0 | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport org.codehaus.groovy.runtime.StringGroovyMethods
public class StringGroovyMethods extends DefaultGroovyMethodsSupport
This class defines new groovy methods which appear on String-related JDK
classes (String, CharSequence, Matcher) inside the Groovy environment.
Static methods are used with the
first parameter being the destination class,
e.g.. public static String reverse(String self)
provides a reverse()
method for String
.
NOTE: While this class contains many 'public' static methods, it is primarily regarded as an internal class (its internal package name suggests this also). We value backwards compatibility of these methods when used within Groovy but value less backwards compatibility at the Java method call level. I.e. future versions of Groovy may remove or move a method call in this file but would normally aim to keep the method available from within Groovy.
Method Summary | |
---|---|
static boolean
|
asBoolean(CharSequence string)
Coerce a string (an instance of CharSequence) to a boolean value. |
static boolean
|
asBoolean(Matcher matcher)
Coerce a Matcher instance to a boolean value. |
static Object
|
asType(CharSequence self, Class c)
|
static Object
|
asType(GString self, Class c)
Converts the GString to a File, or delegates to the default DefaultGroovyMethods.asType |
static Object
|
asType(String self, Class c)
Provides a method to perform custom 'dynamic' type conversion
to the given class using the |
static Pattern
|
bitwiseNegate(CharSequence self)
Turns a CharSequence into a regular expression Pattern |
static Pattern
|
bitwiseNegate(String self)
Turns a String into a regular expression Pattern |
static CharSequence
|
capitalize(CharSequence self)
Convenience method to capitalize the first letter of a CharSequence. |
static String
|
capitalize(String self)
Convenience method to capitalize the first letter of a string (typically the first letter of a word). |
static CharSequence
|
center(CharSequence self, Number numberOfChars)
Pad a CharSequence to a minimum length specified by numberOfChars by adding the space character around it as many times as needed so that it remains centered. |
static CharSequence
|
center(CharSequence self, Number numberOfChars, CharSequence padding)
Pad a CharSequence to a minimum length specified by numberOfChars, appending the supplied padding CharSequence around the original as many times as needed keeping it centered. |
static String
|
center(String self, Number numberOfChars)
Pad a String to a minimum length specified by numberOfChars by adding the space character around it as many times as needed so that it remains centered. |
static String
|
center(String self, Number numberOfChars, String padding)
Pad a String to a minimum length specified by numberOfChars, appending the supplied padding String around the original as many times as needed keeping it centered. |
static String
|
collectReplacements(String orig, Closure transform)
Iterate through this String a character at a time collecting either the original character or a transformed replacement String. |
static boolean
|
contains(CharSequence self, CharSequence text)
Provide an implementation of contains() like Collection.contains to make CharSequences more polymorphic. |
static boolean
|
contains(String self, String text)
Provide an implementation of contains() like Collection.contains to make Strings more polymorphic. |
static int
|
count(CharSequence self, CharSequence text)
Count the number of occurrences of a sub CharSequence. |
static int
|
count(String self, String text)
Count the number of occurrences of a substring. |
static CharSequence
|
denormalize(CharSequence self)
Return a CharSequence with lines (separated by LF, CR/LF, or CR) terminated by the platform specific line separator. |
static String
|
denormalize(String self)
Return a String with lines (separated by LF, CR/LF, or CR) terminated by the platform specific line separator. |
static CharSequence
|
drop(CharSequence self, int num)
Drops the given number of chars from the head of this CharSequence if they are available. |
static CharSequence
|
dropWhile(CharSequence self, Closure condition)
Create a suffix of the given CharSequence by dropping as many characters as possible from the front of the original CharSequence such that calling the given closure condition evaluates to true when passed each of the dropped characters. |
static Object
|
eachLine(CharSequence self, Closure closure)
Iterates through this CharSequence line by line. |
static Object
|
eachLine(CharSequence self, int firstLine, Closure closure)
|
static Object
|
eachLine(String self, Closure closure)
|
static Object
|
eachLine(String self, int firstLine, Closure closure)
|
static String
|
eachMatch(CharSequence self, CharSequence regex, Closure closure)
Process each regex group matched substring of the given CharSequence. |
static String
|
eachMatch(CharSequence self, Pattern pattern, Closure closure)
|
static String
|
eachMatch(String self, Pattern pattern, Closure closure)
|
static String
|
eachMatch(String self, String regex, Closure closure)
Expands all tabs into spaces with tabStops of size 8. |
static CharSequence
|
expand(CharSequence self)
|
static CharSequence
|
expand(CharSequence self, int tabStop)
Expands all tabs into spaces with tabStops of size 8. |
static String
|
expand(String self)
|
static String
|
expand(String self, int tabStop)
|
static CharSequence
|
expandLine(CharSequence self, int tabStop)
Expands all tabs into spaces. |
static String
|
expandLine(String self, int tabStop)
|
static CharSequence
|
find(CharSequence self, CharSequence regex)
|
static CharSequence
|
find(CharSequence self, CharSequence regex, Closure closure)
Finds the first occurrence of a compiled regular expression Pattern within a CharSequence. |
static CharSequence
|
find(CharSequence self, Pattern pattern)
|
static CharSequence
|
find(CharSequence self, Pattern pattern, Closure closure)
|
static String
|
find(String self, Pattern pattern)
|
static String
|
find(String self, Pattern pattern, Closure closure)
|
static String
|
find(String self, String regex)
Finds the first occurrence of a regular expression String within a String. |
static String
|
find(String self, String regex, Closure closure)
Returns a (possibly empty) list of all occurrences of a regular expression (in CharSequence format) found within a CharSequence. |
static List
|
findAll(CharSequence self, CharSequence regex)
|
static List
|
findAll(CharSequence self, CharSequence regex, Closure closure)
Returns a (possibly empty) list of all occurrences of a regular expression (in Pattern format) found within a CharSequence. |
static List
|
findAll(CharSequence self, Pattern pattern)
|
static List
|
findAll(CharSequence self, Pattern pattern, Closure closure)
|
static List
|
findAll(String self, Pattern pattern)
|
static List
|
findAll(String self, Pattern pattern, Closure closure)
Finds all occurrences of a compiled regular expression Pattern within a String. |
static List
|
findAll(String self, String regex)
|
static List
|
findAll(String self, String regex, Closure closure)
|
static CharSequence
|
getAt(CharSequence self, Collection indices)
|
static CharSequence
|
getAt(CharSequence text, EmptyRange range)
Support the range subscript operator for CharSequence or StringBuffer with EmptyRange |
static CharSequence
|
getAt(CharSequence text, int index)
Support the range subscript operator for CharSequence or StringBuffer with IntRange |
static CharSequence
|
getAt(CharSequence text, IntRange range)
Support the range subscript operator for CharSequence |
static CharSequence
|
getAt(CharSequence text, Range range)
|
static List
|
getAt(Matcher self, Collection indices)
|
static Object
|
getAt(Matcher matcher, int idx)
|
static String
|
getAt(String self, Collection indices)
Support the range subscript operator for String with EmptyRange |
static String
|
getAt(String text, EmptyRange range)
Support the subscript operator for String. |
static String
|
getAt(String text, int index)
Support the range subscript operator for String with IntRange |
static String
|
getAt(String text, IntRange range)
Support the range subscript operator for String |
static String
|
getAt(String text, Range range)
|
static char[]
|
getChars(CharSequence self)
Converts the given String into an array of characters. |
static char[]
|
getChars(String self)
Find the number of Strings matched to the given Matcher. |
static int
|
getCount(Matcher matcher)
|
static boolean
|
hasGroup(Matcher matcher)
Check whether a Matcher contains a group or not. |
static boolean
|
isAllWhitespace(CharSequence self)
True if a String only contains whitespace characters. |
static boolean
|
isAllWhitespace(String self)
|
static boolean
|
isBigDecimal(CharSequence self)
Determine if a String can be parsed into a BigDecimal. |
static boolean
|
isBigDecimal(String self)
|
static boolean
|
isBigInteger(CharSequence self)
Determine if a String can be parsed into a BigInteger. |
static boolean
|
isBigInteger(String self)
|
static boolean
|
isCase(CharSequence caseValue, Object switchValue)
'Case' implementation for a GString, which simply calls the equivalent method for String. |
static boolean
|
isCase(GString caseValue, Object switchValue)
|
static boolean
|
isCase(Pattern caseValue, Object switchValue)
|
static boolean
|
isCase(String caseValue, Object switchValue)
|
static boolean
|
isDouble(CharSequence self)
Determine if a String can be parsed into a Double. |
static boolean
|
isDouble(String self)
|
static boolean
|
isFloat(CharSequence self)
Determine if a String can be parsed into a Float. |
static boolean
|
isFloat(String self)
|
static boolean
|
isInteger(CharSequence self)
Determine if a String can be parsed into an Integer. |
static boolean
|
isInteger(String self)
|
static boolean
|
isLong(CharSequence self)
Determine if a String can be parsed into a Long. |
static boolean
|
isLong(String self)
|
static boolean
|
isNumber(CharSequence self)
Determine if a String can be parsed into a Number. |
static boolean
|
isNumber(String self)
Returns an Iterator which traverses each match. |
static Iterator
|
iterator(Matcher matcher)
|
static StringBuilder
|
leftShift(CharSequence self, Object value)
Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a CharSequence. |
static StringBuffer
|
leftShift(String self, Object value)
|
static StringBuffer
|
leftShift(StringBuffer self, Object value)
Overloads the left shift operator to provide syntactic sugar for appending to a StringBuilder. |
static StringBuilder
|
leftShift(StringBuilder self, Object value)
Tells whether or not a CharSequence matches the given compiled regular expression Pattern. |
static boolean
|
matches(CharSequence self, Pattern pattern)
Tells whether or not self matches the given compiled regular expression Pattern. |
static boolean
|
matches(String self, Pattern pattern)
Remove a part of a CharSequence by replacing the first occurrence of target within self with '' and returns the result. |
static boolean
|
matchesPartially(Matcher matcher)
Given a matcher that matches a string against a pattern, this method returns true when the string matches the pattern or if a longer string, could match the pattern. |
static CharSequence
|
minus(CharSequence self, Object target)
Remove a part of a String. |
static String
|
minus(String self, Pattern pattern)
|
static String
|
minus(String self, Object target)
|
static CharSequence
|
multiply(CharSequence self, Number factor)
Repeat a String a certain number of times. |
static String
|
multiply(String self, Number factor)
|
static CharSequence
|
next(CharSequence self)
This method is called by the ++ operator for the class String. |
static String
|
next(String self)
|
static CharSequence
|
normalize(CharSequence self)
Return a String with linefeeds and carriage returns normalized to linefeeds. |
static String
|
normalize(String self)
|
static CharSequence
|
padLeft(CharSequence self, Number numberOfChars)
Pad a CharSequence to a minimum length specified by numberOfChars by adding the space character to the left as many times as needed. |
static CharSequence
|
padLeft(CharSequence self, Number numberOfChars, CharSequence padding)
|
static String
|
padLeft(String self, Number numberOfChars)
|
static String
|
padLeft(String self, Number numberOfChars, String padding)
Pad a CharSequence to a minimum length specified by numberOfChars by adding the space character to the right as many times as needed. |
static CharSequence
|
padRight(CharSequence self, Number numberOfChars)
Pad a CharSequence to a minimum length specified by numberOfChars, adding the supplied padding CharSequence as many times as needed to the right. |
static CharSequence
|
padRight(CharSequence self, Number numberOfChars, CharSequence padding)
|
static String
|
padRight(String self, Number numberOfChars)
|
static String
|
padRight(String self, Number numberOfChars, String padding)
Appends the String representation of the given operand to this string. |
static CharSequence
|
plus(CharSequence left, Object value)
Appends a String to the string representation of this number. |
static String
|
plus(Number value, String right)
|
static String
|
plus(String left, Object value)
|
static String
|
plus(String left, CharSequence value)
|
static String
|
plus(StringBuffer left, String value)
|
static CharSequence
|
previous(CharSequence self)
|
static String
|
previous(String self)
|
static void
|
putAt(StringBuffer self, EmptyRange range, Object value)
Support the range subscript operator for StringBuffer. |
static void
|
putAt(StringBuffer self, IntRange range, Object value)
Return the lines of a CharSequence as a List of CharSequence. |
static List
|
readLines(CharSequence self)
|
static List
|
readLines(String self)
|
static CharSequence
|
replaceAll(CharSequence self, CharSequence regex, CharSequence replacement)
Replaces all occurrences of a captured group by the result of a closure on that text. |
static CharSequence
|
replaceAll(CharSequence self, CharSequence regex, Closure closure)
Replaces all substrings of a CharSequence that match the given compiled regular expression with the given replacement. |
static CharSequence
|
replaceAll(CharSequence self, Pattern pattern, CharSequence replacement)
Replaces all occurrences of a captured group by the result of a closure call on that text. |
static String
|
replaceAll(CharSequence self, Pattern pattern, Closure closure)
|
static String
|
replaceAll(String self, Pattern pattern, Closure closure)
|
static String
|
replaceAll(String self, Pattern pattern, String replacement)
|
static String
|
replaceAll(String self, String regex, Closure closure)
Replaces the first substring of this CharSequence that matches the given regular expression with the given replacement. |
static String
|
replaceFirst(CharSequence self, CharSequence regex, CharSequence replacement)
Replaces the first occurrence of a captured group by the result of a closure call on that text. |
static String
|
replaceFirst(CharSequence self, CharSequence regex, Closure closure)
Replaces the first substring of a CharSequence that matches the given compiled regular expression with the given replacement. |
static CharSequence
|
replaceFirst(CharSequence self, Pattern pattern, CharSequence replacement)
Replaces the first occurrence of a captured group by the result of a closure call on that text. |
static String
|
replaceFirst(CharSequence self, Pattern pattern, Closure closure)
|
static String
|
replaceFirst(String self, Pattern pattern, Closure closure)
|
static String
|
replaceFirst(String self, Pattern pattern, String replacement)
|
static String
|
replaceFirst(String self, String regex, Closure closure)
Creates a new CharSequence which is the reverse (backwards) of this string |
static CharSequence
|
reverse(CharSequence self)
|
static String
|
reverse(String self)
|
static void
|
setIndex(Matcher matcher, int idx)
|
static int
|
size(CharSequence text)
Provide the standard Groovy |
static long
|
size(Matcher self)
|
static int
|
size(String text)
|
static int
|
size(StringBuffer buffer)
|
static CharSequence[]
|
split(CharSequence self)
|
static String[]
|
split(GString self)
|
static String[]
|
split(String self)
|
static Object
|
splitEachLine(CharSequence self, CharSequence regex, Closure closure)
Iterates through the given CharSequence line by line, splitting each line using the given separator Pattern. |
static Object
|
splitEachLine(CharSequence self, Pattern pattern, Closure closure)
Iterates through the given String line by line, splitting each line using the given separator Pattern. |
static Object
|
splitEachLine(String self, Pattern pattern, Closure closure)
|
static Object
|
splitEachLine(String self, String regex, Closure closure)
Strip leading spaces from every line in a CharSequence. |
static CharSequence
|
stripIndent(CharSequence self)
Strip numChar leading characters from every line in a CharSequence. |
static CharSequence
|
stripIndent(CharSequence self, int numChars)
|
static String
|
stripIndent(String self)
|
static String
|
stripIndent(String self, int numChars)
|
static CharSequence
|
stripMargin(CharSequence self)
|
static CharSequence
|
stripMargin(CharSequence self, char marginChar)
|
static String
|
stripMargin(CharSequence self, CharSequence marginChar)
|
static String
|
stripMargin(String self)
|
static String
|
stripMargin(String self, char marginChar)
|
static String
|
stripMargin(String self, String marginChar)
|
static CharSequence
|
take(CharSequence self, int num)
|
static CharSequence
|
takeWhile(CharSequence self, Closure condition)
Parse a CharSequence into a BigDecimal |
static BigDecimal
|
toBigDecimal(CharSequence self)
Parse a CharSequence into a BigInteger |
static BigDecimal
|
toBigDecimal(String self)
Parse a String into a BigInteger |
static BigInteger
|
toBigInteger(CharSequence self)
Converts the given string into a Boolean object. |
static BigInteger
|
toBigInteger(String self)
|
static Boolean
|
toBoolean(String self)
|
static Character
|
toCharacter(String self)
Parse a String into a Double |
static Double
|
toDouble(CharSequence self)
Parse a CharSequence into a Float |
static Double
|
toDouble(String self)
Parse a String into a Float |
static Float
|
toFloat(CharSequence self)
Parse a CharSequence into an Integer |
static Float
|
toFloat(String self)
Parse a String into an Integer |
static Integer
|
toInteger(CharSequence self)
Tokenize a CharSequence (with a whitespace as the delimiter). |
static Integer
|
toInteger(String self)
Tokenize a CharSequence based on the given character delimiter. |
static List
|
toList(CharSequence self)
|
static List
|
toList(String self)
|
static Long
|
toLong(CharSequence self)
Converts the given CharSequence into a Set of unique CharSequence of one character. |
static Long
|
toLong(String self)
|
static Set
|
toSet(CharSequence self)
|
static Set
|
toSet(String self)
Parse a String into a Short |
static Short
|
toShort(CharSequence self)
Translates a string by replacing characters from the sourceSet with characters from replacementSet. |
static Short
|
toShort(String self)
|
static List
|
tokenize(CharSequence self)
Tokenize a CharSequence based on the given CharSequence delimiter. |
static List
|
tokenize(CharSequence self, Character token)
Tokenize a String (with a whitespace as the delimiter). |
static List
|
tokenize(CharSequence self, CharSequence token)
|
static List
|
tokenize(String self)
|
static List
|
tokenize(String self, Character token)
Converts the given CharSequence into a List of CharSequence of one character. |
static List
|
tokenize(String self, String token)
Converts the given String into a List of strings of one character. |
static CharSequence
|
tr(CharSequence self, CharSequence sourceSet, CharSequence replacementSet)
|
static String
|
tr(String self, String sourceSet, String replacementSet)
Replaces sequences of whitespaces with tabs using tabStops of size 8. |
static CharSequence
|
unexpand(CharSequence self)
Replaces sequences of whitespaces with tabs using tabStops of size 8. |
static CharSequence
|
unexpand(CharSequence self, int tabStop)
Replaces sequences of whitespaces with tabs. |
static String
|
unexpand(String self)
|
static String
|
unexpand(String self, int tabStop)
|
static CharSequence
|
unexpandLine(CharSequence self, int tabStop)
Replaces sequences of whitespaces with tabs within a line. |
static String
|
unexpandLine(String self, int tabStop)
|
Methods inherited from class Object | |
---|---|
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
Method Detail |
---|
public static boolean asBoolean(CharSequence string)
string
- the character sequence
public static boolean asBoolean(Matcher matcher)
matcher
- the matcher
public static Object asType(CharSequence self, Class c)
Provides a method to perform custom 'dynamic' type conversion
to the given class using the as
operator.
self
- a CharSequencec
- the desired class
@SuppressWarnings("unchecked") public static Object asType(GString self, Class c)
self
- a GStringc
- the desired class
@SuppressWarnings("unchecked") public static Object asType(String self, Class c)
as
operator.
Example: '123' as Double
By default, the following types are supported:
self
- a Stringc
- the desired class
public static Pattern bitwiseNegate(CharSequence self)
self
- a String to convert into a regular expression
public static Pattern bitwiseNegate(String self)
self
- a String to convert into a regular expression
public static CharSequence capitalize(CharSequence self)
self
- The CharSequence to capitalize
public static String capitalize(String self)
assert 'h'.capitalize() == 'H' assert 'hello'.capitalize() == 'Hello' assert 'hello world'.capitalize() == 'Hello world' assert 'Hello World' == 'hello world'.split(' ').collect{ it.capitalize() }.join(' ')
self
- The string to capitalize
public static CharSequence center(CharSequence self, Number numberOfChars)
self
- a CharSequence objectnumberOfChars
- the total minimum number of characters of the resulting CharSequence
public static CharSequence center(CharSequence self, Number numberOfChars, CharSequence padding)
self
- a CharSequence objectnumberOfChars
- the total minimum number of characters of the resulting CharSequencepadding
- the characters used for padding
public static String center(String self, Number numberOfChars)
['A', 'BB', 'CCC', 'DDDD'].each{ println '|' + it.center(6) + '|' }will produce output like:
| A | | BB | | CCC | | DDDD |
self
- a String objectnumberOfChars
- the total minimum number of characters of the resulting string
public static String center(String self, Number numberOfChars, String padding)
['A', 'BB', 'CCC', 'DDDD'].each{ println '|' + it.center(6, '+') + '|' }will produce output like:
|++A+++| |++BB++| |+CCC++| |+DDDD+|
self
- a String objectnumberOfChars
- the total minimum number of characters of the resulting stringpadding
- the characters used for padding
public static String collectReplacements(String orig, Closure transform)
transform
Closure should return null
to indicate that no transformation is
required for the given character.
assert "Groovy".collectReplacements{ it == 'o' ? '_O_' : null } == 'Gr_O__O_vy' assert "B&W".collectReplacements{ it == '&' ? '&' : null } == 'B&W'
orig
- the original Stringtransform
Closure.
public static boolean contains(CharSequence self, CharSequence text)
self
- a CharSequencetext
- the CharSequence to look for
public static boolean contains(String self, String text)
self
- a Stringtext
- a String to look for
public static int count(CharSequence self, CharSequence text)
self
- a CharSequencetext
- a sub CharSequence
public static int count(String self, String text)
self
- a Stringtext
- a substring
public static CharSequence denormalize(CharSequence self)
self
- a CharSequence object
public static String denormalize(String self)
self
- a String object
public static CharSequence drop(CharSequence self, int num)
def text = "Groovy" assert text.drop( 0 ) == 'Groovy' assert text.drop( 2 ) == 'oovy' assert text.drop( 7 ) == ''
self
- the original CharSequencenum
- the number of characters to drop from this iteratornum
ones,
or else an empty String, if this CharSequence has less than num
characters.
public static CharSequence dropWhile(CharSequence self, Closure condition)
def text = "Groovy" assert text.dropWhile{ false } == 'Groovy' assert text.dropWhile{ true } == '' assert text.dropWhile{ it < 'Z' } == 'roovy' assert text.dropWhile{ it != 'v' } == 'vy'
self
- the original CharSequencecondition
- the closure that while continuously evaluating to true will cause us to drop elements from
the front of the original CharSequence
public static Object eachLine(CharSequence self, Closure closure)
self
- a CharSequenceclosure
- a closure
public static Object eachLine(CharSequence self, int firstLine, Closure closure)
public static Object eachLine(String self, Closure closure)
public static Object eachLine(String self, int firstLine, Closure closure)
public static String eachMatch(CharSequence self, CharSequence regex, Closure closure)
self
- the source CharSequenceregex
- a Regex CharSequenceclosure
- a closure with one parameter or as much parameters as groups
public static String eachMatch(CharSequence self, Pattern pattern, Closure closure)
public static String eachMatch(String self, Pattern pattern, Closure closure)
public static String eachMatch(String self, String regex, Closure closure)
self
- A CharSequence to expand
public static CharSequence expand(CharSequence self)
public static CharSequence expand(CharSequence self, int tabStop)
self
- A String to expand
public static String expand(String self)
public static String expand(String self, int tabStop)
public static CharSequence expandLine(CharSequence self, int tabStop)
self
- A line to expandtabStop
- The number of spaces a tab represents
public static String expandLine(String self, int tabStop)
public static CharSequence find(CharSequence self, CharSequence regex)
public static CharSequence find(CharSequence self, CharSequence regex, Closure closure)
self
- a CharSequencepattern
- the compiled regex Pattern
public static CharSequence find(CharSequence self, Pattern pattern)
public static CharSequence find(CharSequence self, Pattern pattern, Closure closure)
public static String find(String self, Pattern pattern)
public static String find(String self, Pattern pattern, Closure closure)
public static String find(String self, String regex)
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})/)
self
- a Stringregex
- the capturing regex
public static String find(String self, String regex, Closure closure)
self
- a CharSequenceregex
- the capturing regex CharSequence
public static List findAll(CharSequence self, CharSequence regex)
public static List findAll(CharSequence self, CharSequence regex, Closure closure)
self
- a CharSequencepattern
- the compiled regex Pattern
public static List findAll(CharSequence self, Pattern pattern)
public static List findAll(CharSequence self, Pattern pattern, Closure closure)
public static List findAll(String self, Pattern pattern)
public static List findAll(String self, Pattern pattern, Closure closure)
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" }
self
- a Stringpattern
- the compiled regex Patternclosure
- will be passed the full match plus each of the capturing groups
public static List findAll(String self, String regex)
public static List findAll(String self, String regex, Closure closure)
public static CharSequence getAt(CharSequence self, Collection indices)
public static CharSequence getAt(CharSequence text, EmptyRange range)
text
- a CharSequencerange
- an EmptyRange
public static CharSequence getAt(CharSequence text, int index)
text
- a CharSequencerange
- an IntRange
public static CharSequence getAt(CharSequence text, IntRange range)
text
- a CharSequencerange
- a Range
public static CharSequence getAt(CharSequence text, Range range)
public static List getAt(Matcher self, Collection indices)
public static Object getAt(Matcher matcher, int idx)
public static String getAt(String self, Collection indices)
text
- a Stringrange
- an EmptyRange
public static String getAt(String text, EmptyRange range)
text
- a Stringindex
- the index of the Character to get
public static String getAt(String text, int index)
text
- a Stringrange
- an IntRange
public static String getAt(String text, IntRange range)
text
- a Stringrange
- a Range
public static String getAt(String text, Range range)
public static char[] getChars(CharSequence self)
self
- a String
public static char[] getChars(String self)
matcher
- a Matcher
public static int getCount(Matcher matcher)
public static boolean hasGroup(Matcher matcher)
matcher
- a Matchertrue
if matcher contains at least one group.
public static boolean isAllWhitespace(CharSequence self)
self
- The String to check the characters in
public static boolean isAllWhitespace(String self)
public static boolean isBigDecimal(CharSequence self)
self
- a String
public static boolean isBigDecimal(String self)
public static boolean isBigInteger(CharSequence self)
self
- a String
public static boolean isBigInteger(String self)
public static boolean isCase(CharSequence caseValue, Object switchValue)
caseValue
- the case valueswitchValue
- the switch value
public static boolean isCase(GString caseValue, Object switchValue)
public static boolean isCase(Pattern caseValue, Object switchValue)
public static boolean isCase(String caseValue, Object switchValue)
public static boolean isDouble(CharSequence self)
self
- a String
public static boolean isDouble(String self)
public static boolean isFloat(CharSequence self)
self
- a String
public static boolean isFloat(String self)
public static boolean isInteger(CharSequence self)
self
- a String
public static boolean isInteger(String self)
public static boolean isLong(CharSequence self)
self
- a String
public static boolean isLong(String self)
public static boolean isNumber(CharSequence self)
self
- a String
public static boolean isNumber(String self)
matcher
- a Matcher object
public static Iterator iterator(Matcher matcher)
public static StringBuilder leftShift(CharSequence self, Object value)
self
- a CharSequencevalue
- an Object
public static StringBuffer leftShift(String self, Object value)
public static StringBuffer leftShift(StringBuffer self, Object value)
self
- a StringBuildervalue
- an Object
public static StringBuilder leftShift(StringBuilder self, Object value)
self
- the CharSequence that is to be matchedpattern
- the regex Pattern to which the string of interest is to be matched
public static boolean matches(CharSequence self, Pattern pattern)
self
- the string that is to be matchedpattern
- the regex Pattern to which the string of interest is to be matched
public static boolean matches(String self, Pattern pattern)
self
- a CharSequencetarget
- an object representing the part to remove
public static boolean matchesPartially(Matcher matcher)
def emailPattern = /\w+@\w+\.\w{2,}/ def matcher = "john@doe" =~ emailPattern assert matcher.matchesPartially() matcher = "john@doe.com" =~ emailPattern assert matcher.matchesPartially() matcher = "john@@" =~ emailPattern assert !matcher.matchesPartially()
matcher
- the Matcher
public static CharSequence minus(CharSequence self, Object target)
self
- a Stringpattern
- a Pattern representing the part to remove
public static String minus(String self, Pattern pattern)
public static String minus(String self, Object target)
public static CharSequence multiply(CharSequence self, Number factor)
self
- a String to be repeatedfactor
- the number of times the String should be repeated
public static String multiply(String self, Number factor)
public static CharSequence next(CharSequence self)
self
- a String
public static String next(String self)
public static CharSequence normalize(CharSequence self)
self
- a String object
public static String normalize(String self)
public static CharSequence padLeft(CharSequence self, Number numberOfChars)
self
- a CharSequence objectnumberOfChars
- the total minimum number of characters of the resulting CharSequence
public static CharSequence padLeft(CharSequence self, Number numberOfChars, CharSequence padding)
public static String padLeft(String self, Number numberOfChars)
public static String padLeft(String self, Number numberOfChars, String padding)
self
- a CharSequence objectnumberOfChars
- the total minimum number of characters of the resulting string
public static CharSequence padRight(CharSequence self, Number numberOfChars)
self
- a CharSequence objectnumberOfChars
- the total minimum number of characters of the resulting CharSequencepadding
- the characters used for padding
public static CharSequence padRight(CharSequence self, Number numberOfChars, CharSequence padding)
public static String padRight(String self, Number numberOfChars)
public static String padRight(String self, Number numberOfChars, String padding)
left
- a CharSequencevalue
- any Object
public static CharSequence plus(CharSequence left, Object value)
value
- a Numberright
- a String
public static String plus(Number value, String right)
public static String plus(String left, Object value)
public static String plus(String left, CharSequence value)
public static String plus(StringBuffer left, String value)
public static CharSequence previous(CharSequence self)
public static String previous(String self)
public static void putAt(StringBuffer self, EmptyRange range, Object value)
self
- a StringBufferrange
- a Rangevalue
- the object that's toString() will be inserted
public static void putAt(StringBuffer self, IntRange range, Object value)
self
- a CharSequence object
public static List readLines(CharSequence self)
public static List readLines(String self)
public static CharSequence replaceAll(CharSequence self, CharSequence regex, CharSequence replacement)
self
- a CharSequenceregex
- the capturing regexclosure
- the closure to apply on each captured group
public static CharSequence replaceAll(CharSequence self, CharSequence regex, Closure closure)
self
- the CharSequence that is to be matchedpattern
- the regex Pattern to which the CharSequence of interest is to be matchedreplacement
- the CharSequence to be substituted for the first match
public static CharSequence replaceAll(CharSequence self, Pattern pattern, CharSequence replacement)
self
- a CharSequencepattern
- the capturing regex Patternclosure
- the closure to apply on each captured group
public static String replaceAll(CharSequence self, Pattern pattern, Closure closure)
public static String replaceAll(String self, Pattern pattern, Closure closure)
public static String replaceAll(String self, Pattern pattern, String replacement)
public static String replaceAll(String self, String regex, Closure closure)
self
- a CharSequenceregex
- the capturing regexreplacement
- the capturing regex
public static String replaceFirst(CharSequence self, CharSequence regex, CharSequence replacement)
self
- a CharSequenceregex
- the capturing regexclosure
- the closure to apply on the first captured group
public static String replaceFirst(CharSequence self, CharSequence regex, Closure closure)
self
- the CharSequence that is to be matchedpattern
- the regex Pattern to which the CharSequence of interest is to be matchedreplacement
- the CharSequence to be substituted for the first match
public static CharSequence replaceFirst(CharSequence self, Pattern pattern, CharSequence replacement)
self
- a CharSequencepattern
- the capturing regex Patternclosure
- the closure to apply on the first captured group
public static String replaceFirst(CharSequence self, Pattern pattern, Closure closure)
public static String replaceFirst(String self, Pattern pattern, Closure closure)
public static String replaceFirst(String self, Pattern pattern, String replacement)
public static String replaceFirst(String self, String regex, Closure closure)
self
- a CharSequence
public static CharSequence reverse(CharSequence self)
public static String reverse(String self)
public static void setIndex(Matcher matcher, int idx)
public static int size(CharSequence text)
size()
method for CharSequence
.
text
- a CharSequence
public static long size(Matcher self)
public static int size(String text)
public static int size(StringBuffer buffer)
public static CharSequence[] split(CharSequence self)
public static String[] split(GString self)
public static String[] split(String self)
public static Object splitEachLine(CharSequence self, CharSequence regex, Closure closure)
self
- a CharSequencepattern
- the regular expression Pattern for the delimiterclosure
- a closure
public static Object splitEachLine(CharSequence self, Pattern pattern, Closure closure)
self
- a Stringpattern
- the regular expression Pattern for the delimiterclosure
- a closure
public static Object splitEachLine(String self, Pattern pattern, Closure closure)
public static Object splitEachLine(String self, String regex, Closure closure)
self
- The CharSequence to strip the leading spaces from
public static CharSequence stripIndent(CharSequence self)
self
- The CharSequence to strip the characters fromnumChars
- The number of characters to strip
public static CharSequence stripIndent(CharSequence self, int numChars)
public static String stripIndent(String self)
public static String stripIndent(String self, int numChars)
public static CharSequence stripMargin(CharSequence self)
public static CharSequence stripMargin(CharSequence self, char marginChar)
public static String stripMargin(CharSequence self, CharSequence marginChar)
public static String stripMargin(String self)
public static String stripMargin(String self, char marginChar)
public static String stripMargin(String self, String marginChar)
public static CharSequence take(CharSequence self, int num)
public static CharSequence takeWhile(CharSequence self, Closure condition)
self
- a CharSequence
public static BigDecimal toBigDecimal(CharSequence self)
self
- a CharSequence
public static BigDecimal toBigDecimal(String self)
self
- a String
public static BigInteger toBigInteger(CharSequence self)
self
- a String
public static BigInteger toBigInteger(String self)
public static Boolean toBoolean(String self)
public static Character toCharacter(String self)
self
- a String
public static Double toDouble(CharSequence self)
self
- a CharSequence
public static Double toDouble(String self)
self
- a String
public static Float toFloat(CharSequence self)
self
- a CharSequence
public static Float toFloat(String self)
self
- a String
public static Integer toInteger(CharSequence self)
self
- a CharSequence
public static Integer toInteger(String self)
self
- a CharSequencetoken
- the delimiter
public static List toList(CharSequence self)
public static List toList(String self)
public static Long toLong(CharSequence self)
self
- a CharSequence
public static Long toLong(String self)
public static Set toSet(CharSequence self)
public static Set toSet(String self)
self
- a String
public static Short toShort(CharSequence self)
self
- the CharSequence that is to be translatedsourceSet
- the set of characters to translate fromreplacementSet
- the set of replacement characters
public static Short toShort(String self)
public static List tokenize(CharSequence self)
self
- a CharSequencetoken
- the delimiter
public static List tokenize(CharSequence self, Character token)
self
- a String
public static List tokenize(CharSequence self, CharSequence token)
@SuppressWarnings* @param token the delimiter public static List tokenize(String self)
public static List tokenize(String self, Character token)
self
- a CharSequence
@SuppressWarningsfor (int i = 0; i < size; i++) { public static List tokenize(String self, String token)
self
- a String
public static CharSequence tr(CharSequence self, CharSequence sourceSet, CharSequence replacementSet)
public static String tr(String self, String sourceSet, String replacementSet)
self
- A CharSequence to unexpand
public static CharSequence unexpand(CharSequence self)
self
- A String to unexpand
public static CharSequence unexpand(CharSequence self, int tabStop)
self
- A String to unexpandtabStop
- The number of spaces a tab represents
public static String unexpand(String self)
public static String unexpand(String self, int tabStop)
public static CharSequence unexpandLine(CharSequence self, int tabStop)
self
- A line to unexpandtabStop
- The number of spaces a tab represents
public static String unexpandLine(String self, int tabStop)
Copyright © 2003-2013 The Codehaus. All rights reserved.