String function calls

5 minutes to read Download PDF Edit

These are functions to convert and inspect strings. Note that these functions never change the string itself, they only return a new value.

Strings are surrounded by quotes. If the string contains a quote, it should be escaped by another quote. Example: ‘this isn”t funny’

toLowerCase

Set all characters in the string to lowercase.

Input parameters

  • String to convert Type: String

Output

Same string, only all lowercase.

toLowerCase('thisISmyString')

returns:

'thisismystring'

toUpperCase

Set all characters in the string to uppercase.

Input parameters

  • String to convert Type: String

Output

Same string, only all uppercase.

toUpperCase('thisISmyString')

returns:

'THISISMYSTRING'

length

Determines the length of a string.

Input parameters

  • a string Type: String

Output

Length of the string Type: Integer

length('thisismystring')

returns:

14

substring

Retrieve a substring of a string. Note that the first character of a string is located at position ‘0’, and the last character is located at position length(string)-1

Input parameters

  • Subject Type: String
  • Start position of the substring Type: Integer
  • (Optional) Desired length of the result Type: Integer

Output

A part of the original string, starting at start position with length equal to the desired length. If no desired length is specified, will return a substring starting at start position and ending at the end of the string. Type: String

substring('thisismystring', 6)

returns:

'mystring'

or, with a third parameter:

substring('thisismystring', 6,2)

returns:

'my'

find

Find the position of the first occurrence of the substring in the string.

Input parameters

  • Original string. This is the string that you want to search in. Type: String
  • Substring that you want to search for. Type: String
  • (Optional) Start location to begin search from Type: Integer

Output

The first location of the substring in the original string. Will return ‘-1’ if the substring does not occur at all in the original string. Type: Integer

find('thisismystring', 'my')

returns:

6

Substring that doesn’t occur in the original string:

find('thisismystring', 'yourstring')

returns:

-1

With third parameter:

find('thisismystring', 'i', 5)

returns:

11

findLast

Find the position of the last occurrence of substring in the original string.

Input parameters

  • Original string. This is the string that you want to search in. Type: String
  • Substring that you want to search for. Type: String
  • (Optional) Last location to be searched Type: Integer

Output

The first location of the substring in the original string. Will return ‘-1’ if the substring does not occur at all in the original string. Type: Integer

findLast('thisismystring', 't')

returns:

9

Substring that doesn’t occur in the original string:

findLast('thisismystring', 'yourstring')

returns:

-1

With third parameter:

findLast('thisismystring', 'i', 5)

returns:

4

contains

Determines whether a substring occurs in the original string. (Functionally, this method matches the statement find(‘mystring’, ‘otherstring’) = -1)

Input parameters

  • Original string. This is the string that you want to search in. Type: String
  • Substring that you want to search for. Type: String

Output

Whether the original string contains the substring Type: Boolean

contains('thisismystring', 'my')

returns:

true

trim

Removes all whitespace at the beginning and end of a string. (leaves all spaces ‘inside’ the string)

Input parameters

  • A string Type: String

Output

Same string but without spaces at the beginning and end. Type: String

trim(' this is my string     ')

returns:

'this is my string'

isMatch

Checks to see if a string matches a given regular expression

Input parameters

  • String to try and match on Type: String
  • Regular expression to match Type: String

Output

Whether the string matches or not. Type: Boolean

This examples tests to see whether the string contains only numbers

isMatch('234hello6432', '^([0-9]+)$')

returns:

False

replaceAll

Replaces all occurrences of a regular expression with another string.

Input parameters

  • The string to search in Type: String
  • The regular expression to match on Type: String
  • The replacement value Type: String

Output

The original string, with all occurrences of the regular expression replaced by the replacement string. If the regular expression does not occur in the string, the original is returned.

replaceAll('this is a string with 75 some numbers 234 thrown in', '([0-9])', 'NUMBER')

returns:

'this is a string with NUMBERNUMBER some numbers NUMBERNUMBERNUMBER thrown in'

no matches:

replaceAll('this is a string with no numbers thrown in', '([0-9])', 'NUMBER')

returns:

'this is a string with no numbers thrown in'

replaceFirst

Replaces the first occurrence of the regular expression with a replacement string.

Input parameters

  • The string to search in Type: String
  • The regular expression to match on Type: String
  • The replacement value Type: String

Output

The original string, with all occurrences of the regular expression replaced by the replacement string. If the regular expression does not occur in the string, the original is returned.

replaceFirst('this is a string with 75 some numbers 234 thrown in', '([0-9])', 'NUMBER')

returns:

'this is a string with NUMBER5 some numbers 234 thrown in'

startsWith

Determines whether a string starts with the specified substring.

Input parameters

  • The string to search in Type: String
  • The string to match on Type: String

Output

The expression returns a boolean value. True if the input string start with the secondary input, false if it does not. 

startsWith('cookies are amazing', 'cookies')

returns:

true

Input parameters

  • The string to search in Type: String
  • The regular expression to match on Type: String
  • The replacement value Type: String

Output

The original string, with all occurrences of the regular expression replaced by the replacement string. If the regular expression does not occur in the string, the original is returned.

replaceFirst('this is a string with 75 some numbers 234 thrown in', '([0-9])', 'NUMBER')

returns:

'this is a string with NUMBER5 some numbers 234 thrown in'