Package: java.lang

## [Java] Class String

- java.lang.String

-     public class String
      extends Object

  GDK enhancements for String.

  displayName:
    String
  receiverFqcn:
    java.lang.String

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `<T>` | `public T` | `asType(Class<T> c)` Provides a method to perform custom 'dynamic' type conversion to the given class using the `as` operator.
    `` | `public String` | `collectReplacements(Closure<String> transform)` Iterates through this String a character at a time collecting either the original character or a transformed replacement String.
    `` | `public String` | `collectReplacements(List<Function<Character, Optional<String>>> transforms)` Iterates through this String a character at a time collecting either the original character or a transformed replacement String.
    `` | `public byte[]` | `decodeBase64()` Decode the String from Base64 into a byte array.
    `` | `public byte[]` | `decodeBase64Url()` Decodes a Base64 URL and Filename Safe encoded String into a byte array.
    `` | `public byte[]` | `decodeHex()` Decodes a hex string to a byte array.
    `` | `public String` | `drop(int num)` A String variant of the equivalent CharSequence method.
    `` | `public String` | `dropRight(int num)` A String variant of the equivalent CharSequence method CharSequence.dropRight.
    `` | `public String` | `eachMatch(Pattern pattern, Closure closure)` Processes each regex group matched substring of the given pattern.
    `` | `public String` | `eachMatch(String regex, Closure closure)` Process each regex group matched substring of the given string.
    `` | `public Process` | `execute()` Executes the command specified by `self` as a command-line process.
    `` | `public Process` | `execute(String[] envp, File dir)` Executes the command specified by `self` with environment defined by `envp` and under the working directory `dir`.
    `` | `public Process` | `execute(List envp, File dir)` Executes the command specified by `self` with environment defined by `envp` and under the working directory `dir`.
    `` | `public Process` | `execute(Map<String, Object> options)` Executes the command specified by `self` with options provided as named parameters.
    `` | `public String` | `getAt(int index)` Supports the subscript operator for String.
    `` | `public String` | `getAt(IntRange range)` Supports the range subscript operator for String with IntRange.
    `` | `public String` | `getAt(Range range)` Supports the range subscript operator for String.
    `` | `public Boolean` | `isAtLeast(String right)` Compares a String representing a number to another.
    `` | `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.
    `` | `public String` | `plus(CharSequence right)` Appends the String representation of the given operand to this string.
    `` | `public String` | `take(int num)` A String variant of the equivalent CharSequence method.
    `` | `public String` | `takeAfter(CharSequence searchString)` A String variant of the equivalent CharSequence method CharSequence.takeAfter.
    `` | `public String` | `takeBefore(String searchString)` A String variant of the equivalent CharSequence method CharSequence.takeBefore.
    `` | `public String` | `takeBetween(CharSequence from, CharSequence to)` A String variant of the equivalent CharSequence method CharSequence.takeBetween.
    `` | `public String` | `takeBetween(CharSequence enclosure)` A String variant of the equivalent CharSequence method CharSequence.takeBetween.
    `` | `public String` | `takeBetween(CharSequence from, CharSequence to, int occurrence)` A String variant of the equivalent CharSequence method CharSequence.takeBetween.
    `` | `public String` | `takeBetween(CharSequence enclosure, int occurrence)` A String variant of the equivalent CharSequence method CharSequence.takeBetween.
    `` | `public String` | `takeRight(int num)` A GString variant of the equivalent CharSequence method CharSequence.takeRight.
    `` | `public Boolean` | `toBoolean()` Converts the given string into a Boolean object.
    `` | `public Character` | `toCharacter()` Converts the given string into a Character object using the first character in the string.
    `` | `public ProcessBuilder` | `toProcessBuilder()` Creates a ProcessBuilder from a command line String, tokenized on whitespace.
    `` | `public URI` | `toURI()` Transforms a String representing a URI into a URI object.
    `` | `public URL` | `toURL()` Transforms a String representing a URL into a URL object.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `addShutdownHook, any, any, asBoolean, asType, collect, collect, collect, dump, each, eachMatch, eachMatch, eachWithIndex, every, every, find, find, findAll, findAll, findIndexOf, findIndexOf, findIndexValues, findIndexValues, findLastIndexOf, findLastIndexOf, findResult, findResult, findResult, findResult, getAt, getMetaClass, getMetaPropertyValues, getProperties, grep, grep, hasProperty, identity, inject, inject, inspect, invokeMethod, is, isCase, isNotCase, iterator, metaClass, print, print, printf, printf, println, println, println, putAt, respondsTo, respondsTo, setMetaClass, sleep, sleep, split, sprintf, sprintf, stream, tap, toString, use, use, use, with, with, withCloseable, withCloseable, withMethodClosure, withStream, withStream, withTraits`

- - ### Method Detail

    - #### <T> public T asType(Class<T> 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
      - Long
      - Integer
      - Short
      - Byte
      - Character
      - Double
      - Float
      - File
      - Subclasses of Enum
      If any other type is given, the call is delegated to DefaultGroovyMethods.asType.
      Parameters:
        `c` - the desired class
      Returns:
        the converted object
      Since:
        1.0

    - #### public String collectReplacements(Closure<String> transform)

      Iterates through this String a character at a time collecting either the original character or a transformed replacement String. The `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 "Groovy".collectReplacements{ it.equalsIgnoreCase('O') ? '_O_' : null } == 'Gr_O__O_vy'
           assert "Groovy".collectReplacements{ char c -> c == 'o' ? '_O_' : null } == 'Gr_O__O_vy'
           assert "Groovy".collectReplacements{ Character c -> c == 'o' ? '_O_' : null } == 'Gr_O__O_vy'
           assert "B&W".collectReplacements{ it == '&' ? '&' : null } == 'B&W'

      Returns:
        A new string in which all characters that require escaping have been replaced with the corresponding replacements as determined by the `transform` Closure.
      Since:
        2.1.0

    - #### public String collectReplacements(List<Function<Character, Optional<String>>> transforms)

      Iterates through this String a character at a time collecting either the original character or a transformed replacement String. The return value is an `Optional` either having a value equal to the transformed replacement String or `empty()` to indicate that no transformation is required.

           import java.util.function.Function
           import static java.util.Optional.*

           Function<Character, Optional<String>> xform1 = s -> s == 'o' ? of('_O') : empty()
           Function<Character, Optional<String>> xform2 = { it == 'G' ? of('G_') : empty() }
           assert "Groovy".collectReplacements([xform1, xform2]) == 'G_r_O_Ovy'

      Parameters:
        `transforms` - one or more transforms which potentially convert a single character to a transformed string
      Returns:
        A new string in which all characters that require escaping have been replaced with the corresponding replacements as determined by the `transform` function.
      Since:
        3.0.6

    - #### public byte[] decodeBase64()

      Decode the String from Base64 into a byte array.

      Returns:
        the decoded bytes as an array
      Since:
        1.0

    - #### public byte[] decodeBase64Url()

      Decodes a Base64 URL and Filename Safe encoded String into a byte array.

      Returns:
        the decoded bytes as an array
      Since:
        2.5.0

    - #### public byte[] decodeHex()

      Decodes a hex string to a byte array. The hex string can contain either upper case or lower case letters.

      throws:
        NumberFormatException If the string contains an odd number of characters or if the characters are not valid hexadecimal values.
      Returns:
        decoded byte array

    - #### public String drop(int num)

      A String variant of the equivalent CharSequence method.

      Parameters:
        `num` - the number of characters to drop from this String
      Returns:
        a String consisting of all characters except the first `num` ones, or else an empty String, if the String has less than `num` characters.
      Since:
        2.5.5
      See Also:
        drop(CharSequence,int)

    - #### public String dropRight(int num)

      A String variant of the equivalent CharSequence method CharSequence.dropRight.

      Parameters:
        `num` - number of characters
      Returns:
        String after removing the right `num` chars and empty of the `num` is greater than the length of the CharSequence
      Since:
        3.0.0

    - #### public String eachMatch(Pattern pattern, Closure closure)

      Processes 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 many parameters as groups
      Returns:
        the source string
      Since:
        1.6.1

    - #### 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 many parameters as groups
      Returns:
        the source string
      Since:
        1.6.0

    - #### 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` or String.toProcessBuilder.

      throws:
        IOException if an IOException occurs.
      Returns:
        the Process which has just started for this command line representation
      Since:
        1.0

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

      throws:
        IOException if an IOException occurs.
      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

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

      throws:
        IOException if an IOException occurs.
      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

    - #### public Process execute(Map<String, Object> options)

      Executes the command specified by `self` with options provided as named parameters. Supported options:

      - dir: (File, Path, or String) the working directory
      - env: (Map) environment variables to add (merged with inherited environment)
      - redirectErrorStream: (boolean) merge stderr into stdout
      - inheritIO: (boolean) inherit the parent process I/O streams
      - outputFile: (File, Path, or String) redirect stdout to a file
      - errorFile: (File, Path, or String) redirect stderr to a file
      - inputFile: (File, Path, or String) redirect stdin from a file
      - appendOutput: (File, Path, or String) append stdout to a file
      - appendError: (File, Path, or String) append stderr to a file

           "make test".execute(dir: new File("/project"), env: [CI: "true"])
           "cmd".execute(redirectErrorStream: true)

      throws:
        IOException if an IOException occurs.
      Parameters:
        `options` - a Map of options to configure the process
      Returns:
        the Process which has just started
      Since:
        6.0.0

    - #### public String getAt(int index)

      Supports the subscript operator for String.

      Parameters:
        `index` - the index of the Character to get
      Returns:
        the Character at the given index
      Since:
        1.0

    - #### public String getAt(IntRange range)

      Supports the range subscript operator for String with IntRange.

      Parameters:
        `range` - an IntRange
      Returns:
        the resulting String
      Since:
        1.0

    - #### public String getAt(Range range)

      Supports the range subscript operator for String.

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

    - #### public Boolean isAtLeast(String right)

      Compares a String representing a number to another. A fluent API style alias for `compareTo` on `BigDecimal`.

      Parameters:
        `right` - a String representing a number
      Returns:
        true if the value represented by left is equal to or bigger than the value represented by right
      Since:
        3.0.1

    - #### 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 Object
      Returns:
        a StringBuffer built from this string
      Since:
        1.0

    - #### public String plus(CharSequence right)

      Appends the String representation of the given operand to this string.

      Parameters:
        `right` - any CharSequence
      Returns:
        the new string with the object appended
      Since:
        2.2

    - #### public String take(int num)

      A String variant of the equivalent CharSequence method.

      Parameters:
        `num` - the number of chars to take from this String
      Returns:
        a String consisting of the first `num` chars, or else the whole String if it has less than `num` elements.
      Since:
        2.5.5

    - #### public String takeAfter(CharSequence searchString)

      A String variant of the equivalent CharSequence method CharSequence.takeAfter.

      Parameters:
        `searchString` - String that is searched in this CharSequence
      Returns:
        String that is after the given searchString and empty string if it does not exist
      Since:
        3.0.0

    - #### public String takeBefore(String searchString)

      A String variant of the equivalent CharSequence method CharSequence.takeBefore.

      Parameters:
        `searchString` - CharSequence that is searched in this CharSequence
      Returns:
        String that is before the given searchString
      Since:
        3.0.0

    - #### public String takeBetween(CharSequence from, CharSequence to)

      A String variant of the equivalent CharSequence method CharSequence.takeBetween.

      Parameters:
        `from` - beginning of search
        `to` - end of search
      Returns:
        String that is in between the given two CharSequences and empty if the unavailable inputs are given
      Since:
        3.0.0

    - #### public String takeBetween(CharSequence enclosure)

      A String variant of the equivalent CharSequence method CharSequence.takeBetween.

      Parameters:
        `enclosure` - Enclosure CharSequence
      Returns:
        String between the 2 subsequent `enclosure` strings
      Since:
        3.0.0

    - #### public String takeBetween(CharSequence from, CharSequence to, int occurrence)

      A String variant of the equivalent CharSequence method CharSequence.takeBetween.

      Parameters:
        `from` - beginning of search
        `to` - end of search
        `occurrence` - nth occurrence that is to be returned. 0 represents first one
      Returns:
        the String that is in between the given nth (specified by occurrence) pair of `from` and `to` CharSequences and empty if the unavailable inputs are given.
      Since:
        3.0.0

    - #### public String takeBetween(CharSequence enclosure, int occurrence)

      A String variant of the equivalent CharSequence method CharSequence.takeBetween.

      Parameters:
        `enclosure` - Enclosure CharSequence
        `occurrence` - nth occurrence being returned
      Returns:
        String between the nth occurrence of pair of `enclosure` strings
      Since:
        3.0.0

    - #### public String takeRight(int num)

      A GString variant of the equivalent CharSequence method CharSequence.takeRight.

      Parameters:
        `num` - the number of chars to take from this CharSequence from the right
      Returns:
        a String consisting of the last `num` chars, or else the whole CharSequence if it has less than `num` elements.
      Since:
        3.0.0

    - #### 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 otherwise it is false.

      Returns:
        The Boolean value
      Since:
        1.0

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

    - #### public ProcessBuilder toProcessBuilder()

      Creates a ProcessBuilder from a command line String, tokenized on whitespace. This bridges Groovy's convenient string syntax with ProcessBuilder's full feature set.

           def pb = "find . -name '*.groovy'".toProcessBuilder()
           pb.directory(new File("/project"))
           pb.redirectErrorStream(true)
           def proc = pb.start()

      Returns:
        a ProcessBuilder ready to be configured further or started
      Since:
        6.0.0

    - #### public URI toURI()

      Transforms a String representing a URI into a URI object.

      throws:
        java.net.URISyntaxException is thrown if the URI is not well-formed.
      Returns:
        a URI
      Since:
        1.0

    - #### public URL toURL()

      Transforms a String representing a URL into a URL object.

      throws:
        java.net.MalformedURLException is thrown if the URL is not well-formed.
      Returns:
        a URL
      Since:
        1.0