This API documentation is for Ballerina 1.2.0. View API documentation for the latest release.

Functions - stringutils

contains

Checks whether the given string contains a particular substring.

equalsIgnoreCase

Checks if two strings are equal ignoring the case of the strings.

hashCode

Returns a hash code for a given string.

lastIndexOf

Returns the last index of the provided substring within a string.

matches

Checks whether the given string matches the provided regex.

replace

Replaces each occurrence of the provided substring inside the provided originalString with the specified replacement string.

replaceAll

Replaces each occurrence of the substrings, which matches the provided regular expression from the given original string value with the provided replacement string.

replaceFirst

Replaces the first substring that matches the given regular expression with the provided replacement string.

split

Returns an array of strings by splitting a string using the provided delimiter.

toBoolean

Returns a boolean value of a given string.

contains

(string originalString, string substring)

returns boolean

Checks whether the given string contains a particular substring.

 boolean contains = stringutils:contains("Ballerina", "in");

Parameters

  • originalString string
  • The string to check whether it contains the substring

  • substring string
  • The substring to find within the originalString

  • Return Type

    (boolean)
  • true if the originalString contains the provided substring or else false

equalsIgnoreCase

(string firstString, string secondString)

returns boolean

Checks if two strings are equal ignoring the case of the strings.

 boolean isEqual = stringutils:equalsIgnoreCase("BaLLerinA", "ballERina");

Parameters

  • firstString string
  • The first string to compare

  • secondString string
  • The second string to compare

  • Return Type

    (boolean)
  • true if the two strings are equal or else false

hashCode

(string stringValue)

returns int

Returns a hash code for a given string.

 int hashCode = stringutils:hashCode("Ballerina");

Parameters

  • stringValue string
  • The string to generate the hash code

  • Return Type

    (int)
  • The hash code for the given string

lastIndexOf

(string originalString, string substring)

returns int

Returns the last index of the provided substring within a string.

 int lastIndex = stringutils:lastIndexOf("int values in Ballerina", "in");

Parameters

  • originalString string
  • The string to search for the index of the substring

  • substring string
  • The string to search in the originalString

  • Return Type

    (int)
  • Starting index of the last appearance of the provided substring if the originalString contains the substring or else -1

matches

(string stringToMatch, string regex)

returns boolean

Checks whether the given string matches the provided regex.

 boolean isMatched = stringutils:matches("Ballerina is great", "Ba[a-z ]+");

Parameters

  • stringToMatch string
  • The string to match the regex

  • regex string
  • The regex to match the string

  • Return Type

    (boolean)
  • true if the provided string matches the regex or else false

replace

(string originalString, string stringToReplace, string replacement)

returns string

Replaces each occurrence of the provided substring inside the provided originalString with the specified replacement string.

 string result = stringutils:replace("Ballerina is great", " ", "_");

Parameters

  • originalString string
  • The original string to replace the substrings

  • stringToReplace string
  • The string to replace within the originalString

  • replacement string
  • The replacement string to replace the occurrences of the stringToReplace

  • Return Type

    (string)
  • The string with the replaced substrings

replaceAll

(string originalString, string regex, string replacement)

returns string

Replaces each occurrence of the substrings, which matches the provided regular expression from the given original string value with the provided replacement string.

 string result = stringutils:replaceAll("Ballerina is great", "\s+", "_");

Parameters

  • originalString string
  • The original string to replace the occurrences of the substrings that match the provided regex

  • regex string
  • The regex to match the substrings in the originalString to be replaced

  • replacement string
  • The replacement string to replace the subsgrings, which match the regex

  • Return Type

    (string)
  • The resultant string with the replaced substrings

replaceFirst

(string originalString, string regex, string replacement)

returns string

Replaces the first substring that matches the given regular expression with the provided replacement string.

 string result = stringutils:replaceFirst("Ballerina is great", "\s+", "_");

Parameters

  • originalString string
  • The original string to replace the occurrences of the substrings that match the provided regex

  • regex string
  • The regex to match the first substring in the originalString to be replaced

  • replacement string
  • The replacement string to replace the first substring, which matches the regex

  • Return Type

    (string)
  • The resultant string with the replaced substring

split

(string receiver, string delimiter)

returns string[]

Returns an array of strings by splitting a string using the provided delimiter.

 string[] result = stringutils:split("Ballerina is great", " ");

Parameters

  • receiver string
  • The string to split

  • delimiter string
  • The delimiter to split by

  • Return Type

    (string[])
  • An array of strings containing the individual strings that are split

toBoolean

(string stringValue)

returns boolean

Returns a boolean value of a given string.

 boolean result = stringutils:toBoolean("true");

Parameters

  • stringValue string
  • string value to convert to a boolean

  • Return Type

    (boolean)
  • true if the string is "true" (without considering the case) or else false