String Functions and Properties

In this Page

String Literals

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

Example:
"Bob" or 'Bob'
 
The only special characters supported are single quotes or double quotes.
Examples:
"\"Bob\" is a literal string "Bob" 
'\'Bob\'' is a literal string 'Bob' 

Static Methods

fromCharCode

Description

Returns a string created by using the specified sequence of Unicode values.

This is similar to the JavaScript fromCharCode.

Syntax
String.fromCharCode
Example

Expression: String.fromCharCode (69, 97, 116, 32, 114, 105, 103, 104, 116)

Result: Eat right

String Functions and Properties

camelCase

Description

Returns the string in camelCase.

This is similar to the Lodash camelCase.

Syntax
string.camelCase()
Example

Expression: $tag.camelCase()

where $tag is "sim_world"

Result: simWorld

capitalize

Description

Returns the first character from a string in upper case and the remaining characters in lower case.

This is similar to the Lodash capitalize.

Syntax
string.capitalize()
Example

Expression: $first.capitalize()

where $first contains "JANE"

Result: Jane

charAt

Description

Returns the character from a string at the integer-specified location.

This is similar to the JavaScript charAt.

Syntax
string.charAt(index)
Example

Expression: $first.charAt(0)

where $first contains John

Result: J

charCodeAt

Description

Returns the numeric Unicode value of the character at the given index.

This is similar to the JavaScript charCodeAt.

Syntax
string.charCodeAt(index)
Example

Expression: $first.charCodeAt(0)

where $first contains "Dan"

Result: 68

concat

Description

Combines the text of two or more strings and returns a new string.

This is similar to the JavaScript concat.

Syntax
string.concat(string2[,...])
Example

Expression: $first.concat($last)

where $first contains "John" and $last contains "Doe"

Result: JohnDoe

Expression: $first.concat(" ", $last)

where $first contains "John" and $last contains "Doe"

Result: John Doe

contains

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

Syntax
$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)

endsWith

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
string.endsWith()
Example

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

Result: true

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

Result: true

indexOf

Description

Returns the index within the calling String object of the first occurrence of the specified value where indexOf will search the searchValue from the 'fromIndex' of string to the end. The index of the first character is 0; -1 returns if the searchValue is not found.

This is similar to the JavaScript indexOf.

Syntax
string.indexOf(searchValue[, fromIndex]) 
Example

Expression: $city.indexOf("San")

where $city is "San Francisco"

Result: 0


Expression: $city.indexOf("Jose", 0)

where $city is "San Jose"

Result: 4

kebabCase

Description

Returns the string in kebabCase.

This is similar to the Lodash kebabCase.

Syntax
string.kebabCase()
Example

Expression: $action.kebabCase()

where $action is "Check_in"

Result: check-in

lastIndexOf

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
string.lastIndexOf(searchValue[, fromIndex])
Example

Expression: $email.lastIndexOf("e")

where $email is "vp@example.com"

Result: 10

length

Description

Returns the number of code units in the string.

This is similar to the JavaScript length.

Syntax
string.length
Example

Expression: $first.length

where $first contains John

Result: 4

localeCompare

Description

Compares this string to another and returns a number that represents the sort order.  The meaning of the number is as follows:

  • negative - This string comes before the given one
  • zero - This string is equal to the given one
  • positive - This string comes after the given one

This is similar to the JavaScript String.localeCompare method.

Syntax
string.localeCompare(compareString) 
Example

Expression: "a".localeCompare("b")

Result: A negative number.

lowerFirst

Description

Returns the string with the first character in lower case.

This is similar to the Lodash lowerFirst.

Syntax
string.lowerFirst()
Example

Expression: $msg.lowerFirst()

where $msg is "FOOBAR"

Result: fOOBAR

match

Description

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

This is similar to the JavaScript match.

Syntax
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:

$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

repeat

Description

Returns a new string containing the specified number of copies of the specified string, concatenated together.

This is similar to the JavaScript repeat.

Syntax
string.repeat(number)
Example

Expression: $string.repeat ("3")

where $string contains "Hello"

Result: HelloHelloHello

replace

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


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!

replaceAll

Description

Returns a new string with all matches of a pattern replaced by another.

This is similar to the JavaScript replace.

Syntax
string.replaceAll(find,replaceWith)
Example

Expression: $msg.replaceAll ("l","1")

where $msg contains "All letters"

Result: A11 1etters


Description

Returns the index of the first match of a regular expression object and a string.

This is similar to the JavaScript search.

Syntax
string.search(regex)
Example

Expression: $String.search("score")

where $String contains "Four score and seven years ago"

Result: 5

slice

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


snakeCase

Description

Returns the string in snakeCase.

This is similar to the Lodash snakeCase.

Syntax
string.snakeCase()
Example

Expression: $category.snakeCase()

where $category is "LevelCritical"

Result: level_critical

split

Description

Returns an array of strings divided at the supplied separator.

This is similar to the JavaScript split.

Syntax
string.split(separator[,limit])

To split at a newline character, use .split('\n').

Example

Expression: $Location.split('/')

where $Location contains "/snaplogic/projects/DocTest"

Result:

[
      "",
      "snaplogic",
      "projects",
      "DocTest"
    ]


Expression: $Location.split('/',3)

where $Location contains "/Snaplogic/projects/DocTest"

Result:

[
      "",
      "Snaplogic",
      "projects"
    ]


Expression: $name.split(' ')

where $name contains:

where $name contains:
 [{, ...}, {name:John Jones}] 
     {name:John Paul Jones} 
         name: "John Paul Jones"
     {name:John Jones} 
         name: "John Jones"

Result:

[{, ...}, {, ...}] 
         {name:{, ...}} 
                name:  [John, Paul, Jones] 
                        "John", 
                        "Paul", 
                        "Jones"

         {name:[John, Jones]} 
                name:  [John, Jones] 
                        "John", 
                        "Jones"

sprintf

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

startsWith

Description

Returns true or false after determining if one string starts with the characters of another.

This is similar to the JavaScript startsWith.

Syntax
string.startsWith(searchString[,position])
Example

Where $string contains "Copyright 2017"

Expression: $string.startsWith("Copyright")

Result: true

Expression: $string.startsWith("All")

Result: False

Expression: $string.startsWith("2017", 10)

Result: true

substr

Description

Returns the characters in a string beginning at the specified location through the specified number of characters.

This is similar to the JavaScript substr.

Syntax
string.substr(start[,length])
Example

Expression: $first.substr(0,1)

where $first contains "John"

Result: J

substring

Description

Returns a subset of a string between one index and another, or through the end of the string.

This is similar to the JavaScript substring.

Syntax
string.substring(start[,end])
Example

Expression: "SnapLogic".substring(4)

Result: Logic

toLowerCase

Description

Returns the value of the string converted to lower case.

This is similar to the JavaScript toLowerCase.

Syntax
string.toLowerCase()
Example

Expression: $login.toLowerCase()

where $login contains FirstLast@email.com.

Result: firstlast@email.com

toUpperCase

Description

Returns the value of the string converted to uppercase.

This is similar to the JavaScript toUpperCase.

Syntax
string.toUpperCase()
Example

Expression: $login.toUpperCase()

where $login contains "FirstLast@email.com".

Result: FIRSTLAST@EMAIL.COM

trim

Description

Returns the string stripped of whitespace from both ends.

This is similar to the JavaScript trim.

Syntax
string.trim()
Example

Expression: $string.trim()

where $string contains the string " test "

Result: test

trimLeft

Description

Returns the string stripped of whitespace from the left end of the string.

This is similar to the JavaScript trimLeft.

Syntax
string.trimLeft()
Example

Expression: $string.trimLeft()

where $string contains the string " test"

Result: test

trimRight

DescriptionReturns the string stripped of whitespace from the right end of the string.
Syntax

This is similar to the JavaScript trimRight.

string.trimRight()
Example

Expression: $string.trimRight()

where $string contains the string "test "

Result: test

upperFirst

Description

Returns the string with the first character in upper case.

This is similar to the Lodash upperFirst.

Syntax
string.upperFirst()
Example

Expression: $msg.upperFirst()

where $msg is "foobar"

Result: Foobar


Examples

 Checking for a Non-empty String

Checking for a Non-empty String

Expression
$customer.lastname.trim() || 'default'
DescriptionThe expression verifies if $customer.lastname and $customer.lastname.trim() has a value. If it does, then the value of $customer.lastname.trim() is applied to the target path; otherwise, 'default' gets applied to the target path.
 Checking for an Existing Value

Checking for an Existing Value

Expression
$.hasOwnProperty('customer.lastname') || 'default'
DescriptionThe expression verifies if $customer.lastname exists in the input document. If it does, then the expression applies its value to the target path; otherwise, it applies 'default' of the input document to the target path.
 Checking for a Non-empty String and an Existing Value

Checking for a Non-empty String and an Existing Value

Expression
($.hasOwnProperty('customer.lastname') && $customer.lastname.trim()) || 'default''
DescriptionThe expression verifies if a property exists in the input document and if $customer.lastname and $customer.lastname.trim() provide a value. If it does, then the expression applies its value to the target path; otherwise, it applies 'default' of the input document to the target path.
 Creating an Email Address from First Initial, Last Name

Creating an Email Address from First Initial, Last Name

Expression
$first_name.substr(0,1).toLowerCase()+$last_name.toLowerCase() + "@example.com"
$first_name.charAt(0).toLowerCase()+$last_name.toLowerCase() + "@example.com"
DescriptionEither expression grabs the first letter of the $first_name field, converts it to lower case, then adds it to the $last_name value and appends "@example.com" before writing it to $Email.