Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this Page

...

String Literals

String literals function the is similar to JavaScript strings.
A string literal can be constructed with either single quotes or double quotes.

...

Description

Returns true or false after determining if one string can be found within another. If position is specified, the search will begin there. Position defaults to 0.

This is similar to the JavaScript containsincludes.

Syntax


Code Block
$string.contains(searchString[, position])


Example

Expression: $msg.contains('Hello')

where $msg contains "Hello, World"

Result: true


To specify a position in the string where to start searching:

Expression: $msg.contains('Hello', 8)

where $msg contains "Hello, World"

Result: false (because "Hello" exists before that position)

...

Description

Returns true or false after determining if one string ends with the characters of another. Optional parameters include searchString and length, where searchString searches for a string of the length specified.  If unspecified, the default is the length of the string entered.

This is similar to the JavaScript endsWith.

Syntax


Code Block
string.endsWith()


Example

Expression: $email.endsWith("@example.com")

Result: true

Expression: $bootstrapping.endsWith(searchString[length, 13])

Result: true

...

Description

Returns the string in kebabCase.

This is similar to the Lodash Lodash kebabCase.

Syntax


Code Block
string.kebabCase()


Example

Expression: $action.kebabCase()

where $action is "Check_in"

Result: check-in

...

Description

Returns the index within the calling String object of the last occurrence of the specified value. The index of the first character is 0; -1 returns if the searchValue is not found.

This is similar to the JavaScript lastIndexOf.

Syntax


Code Block
string.lastIndexOf(searchValue[, fromIndex])


Example

Expression: $email.lastIndexOf("xe")

where $email is "vp@example.com"

Result: 410

length

Description

Returns the number of code units in the string.

This is similar to the JavaScript length.

Syntax


Code Block
string.length


Example

Expression: $first.length

where $first contains John

Result: 4

...

Description

Returns an array of results when matching a string against a regular expression object.

This is similar to the JavaScript match.

Syntax


Code Block
string.match(regexp)


Example

The argument to match() takes a string that will be interpreted as a regular expression. This enables you to create expressions such as the following to perform a global search to match strings in the employee code that contain actual employee ID values, followed by any five digits:

paste-code-macro
$EMPLOYEE_CODE.match($employee_id + "-\\d{5,}")

To match a whole word in a given variable called FirstName:

Expression: $FirstName.match(/\bJohn\b/g)

where \b sets the word boundary and looks for string 'John' in the incoming string variable called $FirstName

Result: John

...

Description

Returns a new string with some or all matches of a pattern replaced by another.   The replacement can be a string or a function that is called for each match.

If the first argument is a regular expression, the method will replace all matches if the global flag is set (e.g. /test/g).  If the global flag is not set or the first argument is a string, only the first match will be replaced.

If the replacement is a function, its result will be used as the replacement for a given match.  The function will be passed the following parameters:

  • match: The substring that was matched by the regular expression.
  • p1, p2, ...: The substrings that were captured by the regular expression.
  • offset: The offset of this match within the original string.
  • string: The original string being replaced.

This is similar to the JavaScript replace.

Syntax


Code Block
string.replace(find,replaceWith)


Example


Expression: $msg.replace ("l","x")

where $msg contains "Hello, World"

Result: Hexlo, World


To ignore case, use the /i flag:

Expression: $msg.replace (/world/i,"my friend")

where $msg contains "Hello, World"

Result: Hello, my friend


To globally replace all instances of the letter "l", use the /g flag:

Expression: $msg.replace (/l/g,"x")

where $msg contains "Hello World"

Result: Hexxo Worxd


Expression: $inputstring.replace (/\//g,"_")

where $inputstring contains "/org/projectspace/project"

Result:  _org_projectspace_project

To pass a callback function for matching strings in the regex A-Z

Expression: $value.replace (/ABC/g,"123")

where $value contains "/org/projectspace/project"

Result:  _org_projectspace_project


To capitalize the words in a string:

Expression: $msg.replace(/\b([a-z])(\w+)\b/g, (_matched, first, rest) => first.toUpperCase() + rest.toLowerCase())

where $msg contains "hello, world!"

Result: Hello, World!


...

Description

Returns a new string with text extracted from another string.

If beginIndex is greater than or equal to the length of the string, an empty string is returned. If it is negative, it returns that many characters from the end of the string length.

If endIndex is omitted, slice() extracts to the end of the string. If it is a positive value, the string value is captured up to, but not including that character. If the value is negative, the capture extracts to that number of characters from the end.

This is similar to the JavaScript slice.

Syntax


Code Block
string.slice(beginIndex[, endIndex])

Example

Where $String contains "Copyright 2017 All rights reserved."

Expression: $String.slice(10)

Result: 2017 All rights reserved.


Expression: $String.slice(10,14)

Result: 2017


Expression: $String.slice(10,-2)

Result: 2017 All rights reserve


Expression: $String.slice(-2)

Result: d.


...

Description

Swaps out a part of the string using placeholders with another. 

This is similar to JavaScript sprintf() but includes extended functionality as described in java.util class formatter.

Syntax


Code Block
string.sprintf()


Example

Where $msg is "hello %s %s"

Expression$msg.sprintf("from", "SnapLogic")

Result"hello from SnapLogic"

Where $msg is "hello %2$s %1$s"

Expression: $msg.sprintf("Snaplogic", "from")

Result: "hello from Snaplogic"

...