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 Example: `'123' as Double`

By default, the following types are supported:

- List
- BigDecimal
- BigInteger
- Character
- Character
- Double
- Float
- File
- Subclasses of Enum (Java 5 and above)

If any other type is given, the call is delegated to {@link #asType(Object,Class)}

`Pattern` | `bitwiseNegate()` Turns a String into a regular expression pattern
`String` | `center(Number numberOfChars, String padding)` Center a String and padd it with the characters appended around it
`String` | `center(Number numberOfChars)` Center a String and padd it with spaces appended around it
`boolean` | `contains(String text)` Provide an implementation of contains() like {@link Collection#contains(Object)} to make Strings more polymorphic This method is not required on JDK 1
`int` | `count(String text)` Count the number of occurencies of a substring
`byte[]` | `decodeBase64()` Decode the Sting from base64 into a byte array
`Object` | `eachLine(Closure closure)` Iterates through this String line by line to the given closure
`void` | `eachMatch(String regex, Closure closure)` Process each regex group matched substring of the given string 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
`Process` | `execute()` Executes the given string as a command line process over the process mechanism in JDK 1
`Process` | `execute(String[] envp, File dir)` Executes the command specified by the `self` with environments `envp` under the working directory `dir` For more control over the process mechanism in JDK 1
`Process` | `execute(List envp, File dir)` Executes the command specified by the `self` with environments `envp` under the working directory `dir` For more control over the process mechanism in JDK 1
`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)` Allows a List to be used as the indices to be used on a String
`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 For example:

    switch( str ) {
    case 'one' :
    // etc
    }

Note that this returns `true` for the case where both the 'switch' and 'case' operand is `null`

`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 Synonym for 'isBigDecimal()'
`StringBuffer` | `leftShift(Object value)` Overloads the left shift operator to provide an easy way to append multiple objects as string representations to a String
`String` | `minus(Object target)` Remove a part of a String of target within self with '' and returns the result target is a regex Pattern, the first occurrence of that pattern will be removed (using regex matching), otherwise the first occurrence of target
`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 It increments the last character in the given string character in the string is Character will be appended consisting of the character Character
`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 It decrements the last character in the given string character in the string is Character The empty string can't be decremented
`List` | `readLines()` Return the lines of a String as a List of Strings

`String`

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

For examples,

    assert "FOOBAR-FOOBAR-" == "foobar-FooBar-"

    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-"

    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

`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 sep, Closure closure)` Iterates through the given String line by line, splitting each line using the given separator the given closure
`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 If the trimmed string is "true", "y" or "1" (ignoring case) then the result is true othewrwise it is false
`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
`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()` 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:

  - List
  - BigDecimal
  - BigInteger
  - Character
  - Character
  - Double
  - Float
  - File
  - Subclasses of Enum (Java 5 and above)
  If any other type is given, the call is delegated to {@link #asType(Object,Class)}.

Parameters:
  `c` - the desired class.
Returns:
  the converted object

### bitwiseNegate

    public Pattern bitwiseNegate()

  Turns a String into a regular expression pattern

Returns:
  the regular expression pattern

### center

    public String center(Number numberOfChars, String padding)

  Center a String and padd 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

### center

    public String center(Number numberOfChars)

  Center a String and padd it with spaces appended around it

Parameters:
  `numberOfChars` - the total number of characters.
Returns:
  the String centered with padded character around

### contains

    public boolean contains(String text)

  Provide an implementation of contains() like {@link Collection#contains(Object)} 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

### 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

### decodeBase64

    public byte[] decodeBase64()

  Decode the Sting from base64 into a byte array.

Returns:
  the decoded bytes as an array

### eachLine

    public Object eachLine(Closure closure)

  Iterates through this String line by line. Each line is passed to the given closure.

Parameters:
  `closure` - a closure.
Returns:
  the self Object

### eachMatch

    public void 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.

### execute

    public Process execute()

  Executes the given string as a command line process. For more control over the process mechanism in JDK 1.5 you can use java.lang.ProcessBuilder.

Returns:
  the Process which has just started for this command line string

### execute

    public Process execute(String[] envp, File dir)

  Executes the command specified by the `self` with environments `envp` under the working directory `dir`. For more control over the process mechanism in JDK 1.5 you can use `java.lang.ProcessBuilder`.

Parameters:
  `envp has name=value, `null` the` - 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 `null` the` - 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 string.

### execute

    public Process execute(List envp, File dir)

  Executes the command specified by the `self` with environments `envp` under the working directory `dir`. For more control over the process mechanism in JDK 1.5 you can use `java.lang.ProcessBuilder`.

Parameters:
  `envp has name=value, `null` the` - a List of Strings, 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 `null` the` - 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 string.

### 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

### getAt

    public String getAt(IntRange range)

  Support the range subscript operator for String with IntRange

Parameters:
  `range` - an IntRange.
Returns:
  the resulting String

### getAt

    public String getAt(EmptyRange range)

  Support the range subscript operator for String with EmptyRange

Parameters:
  `range` - an EmptyRange.
Returns:
  the resulting String

### getAt

    public String getAt(Range range)

  Support the range subscript operator for String

Parameters:
  `range` - a Range.
Returns:
  a substring corresponding to the Range

### getAt

    public String getAt(Collection indices)

  Allows a List to be used as the indices to be used on a String

Parameters:
  `indices` - a Collection of indices.
Returns:
  a String of the values at the given indices

### isBigDecimal

    public boolean isBigDecimal()

  Determine if a String can be parsed into a BigDecimal.

Returns:
  true if the string can be parsed

### isBigInteger

    public boolean isBigInteger()

  Determine if a String can be parsed into a BigInteger.

Returns:
  true if the string can be parsed

### 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

### isDouble

    public boolean isDouble()

  Determine if a String can be parsed into a Double.

Returns:
  true if the string can be parsed

### isFloat

    public boolean isFloat()

  Determine if a String can be parsed into a Float.

Returns:
  true if the string can be parsed

### isInteger

    public boolean isInteger()

  Determine if a String can be parsed into an Integer.

Returns:
  true if the string can be parsed

### isLong

    public boolean isLong()

  Determine if a String can be parsed into a Long.

Returns:
  true if the string can be parsed

### 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

See:
  #isBigDecimal(String).

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### 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

### readLines

    public List readLines()

  Return the lines of a String as a List of Strings.

Returns:
  a list of lines

### replaceAll

    public String replaceAll(String regex, Closure closure)

  Replaces all occurrencies 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

Parameters:
  `regex` - the capturing regex.
  `closure` - the closure to apply on each captured group.
Returns:
  a String with replaced content

### 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.

### size

    public int size()

  Provide the standard Groovy `size()` method for `String`.

Returns:
  the length of the String

### 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

### splitEachLine

    public Object splitEachLine(String sep, 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:
  `sep` - a String separator.
  `closure` - a closure.
Returns:
  the last value returned by the closure

See:
  String#split(String).

### toBigDecimal

    public BigDecimal toBigDecimal()

  Parse a String into a BigDecimal

Returns:
  a BigDecimal

### toBigInteger

    public BigInteger toBigInteger()

  Parse a String into a BigInteger

Returns:
  a BigInteger

### 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

### toCharacter

    public Character toCharacter()

  Converts the given string into a Character object using the first character in the string.

Returns:
  the first Character

### toDouble

    public Double toDouble()

  Parse a String into a Double

Returns:
  a Double

### toFloat

    public Float toFloat()

  Parse a String into a Float

Returns:
  a Float

### toInteger

    public Integer toInteger()

  Parse a String into an Integer

Returns:
  an Integer

### 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)

### toLong

    public Long toLong()

  Parse a String into a Long

Returns:
  a Long

### toURI

    public URI toURI()

  Transforms a String representing a URI into a URI object.

Returns:
  a URI

### toURL

    public URL toURL()

  Transforms a String representing a URL into a URL object.

Returns:
  a URL

### tokenize

    public List tokenize(String token)

  Tokenize a String based on the given string delimiter.

Parameters:
  `token` - the delimiter.
Returns:
  a List of tokens

See:
  StringTokenizer#StringTokenizer.

### tokenize

    public List tokenize()

  Tokenize a String (with a whitespace as the delimiter).

Returns:
  a List of tokens

See:
  StringTokenizer#StringTokenizer.

Groovy JDK