String Functions
toString(variable)
Convert the input variable to a string (string|blob|int64|float64|bool|null|undefined|jsonObj|jsonArray)
toLower(string) ⇒ string
returns the string in lowercase
toLower("HELLO") // return the string "hello"
toLower(" World") // return the string " world"
toUpper(string) ⇒ string
returns the string in uppercase
toUpper("hello") // return the string "HELLO"
toUpper("wORld") // return the string "WORLD"
startsWith(string, prefix) ⇒ bool
returns true if string starts with prefix, false otherwise
is case and whitespace sensitive
let s = "hello"
startsWith("hello", "he") // return true
startsWith("hello", "He") // return false
endsWith(string, suffix) ⇒ bool
returns true if string ends with suffix, false otherwise
is case and whitespace sensitive
let s = "hello"
endsWith("hello", "llo") // return true
endsWith("hello", "LLO") // return false
contains(string, subString) ⇒ bool
returns true if subString exists in string false otherwise
is case and whitespace sensitive
let s = "hello"
contains("hello", "ello") // return true
contains("hello", "hi") // return false
contains("hello", "He") // return false
content(string1, string2) ⇒ bool
returns true if string1 equals string2 false otherwise
is case and whitespace sensitive
let s = "hello"
content(s, "hello") // return true
content(s, "Hello") // return false
content(s, "hello ") // return false
trim(s, cutset) ⇒ string
returns a sliced of the string s with all leading and trailing Unicode code points contained in cutset removed.
cutset will be seen as a collection of characters
let s = "Hello and Hello"
trim(s, "Hello") // return the string "and"
trim(s, "o leH") // return the string "and"
trim(s, "Hel") // return the string "lo and Hello"
trimPrefix(s, prefix) ⇒ string
returns s without the provided leading prefix string. If s doesn’t start with prefix, s is returned unchanged.
is case and whitespace sensitive
let s = "Hello World"
trimPrefix(s, "Hello ") // return the string "World"
trimPrefix(s, "hello") // return the string "Hello World"
trimSuffix(s, suffix) ⇒ string
returns s without the provided trailing suffix string. If s doesn’t end with suffix, s is returned unchanged.
is case and whitespace sensitive
let s = "Hello World"
trimSuffix(s, "World") // return the string "Hello "
trimSuffix(s, "Hello") // return the string "Hello World"
split(variable, delim)
split the input string on delim and returns a list of string
let s = "1,2,3"
split(s, ",") // return a list ["1", "2", "3"]
split(s, "2") // return a list ["1,", ",3"]
split(s, "1") // return a list ["", ",2,3"]
indexOf(s, substring)
returns the index of the first instance of a substring in a given string.
return -1 if the substring is not available.
let s = "abcd"
let i = indexOf(s, "b")
let j = indexOf(s, "n")
printf("i=%d j=%d", i, j)
// i: 1 j:-1
subString(s, start, end)
extracts substring from start to end (exclusion)
let s = "abcd"
let sub = subString(s, 1, 2)
printf("subString=%s", sub)
// subString=b
coalesce(var1, var2, var3, …)
return the first argument that is a non-empty string value, undefined otherwise
coalesce("str1", "str2", "str3", ...) // return the string "str1"
coalesce("", 15, "str3", ...) // return the string "str3"
coalesce("", "", "") // return undefined
replace(s, old, new, count)
returns a copy of the given string, starting with the first 'count' non-overlapping instances of the old string replaced with the new one
- s: the input string
- old: the string to be replaced
- new: the string that replaces the old one
- count: up to the number of times the old string will be replaced. If count is less than zero, no limit on the number of replacement
let s = "a a a"
replace(s, "a", "Hello", 1) // return the string "Hello a a"
replace(s, "a", "Hello", 0) // return the string "a a a"
replace(s, "a", "Hello", -1) // return the string "Hello Hello Hello"
replaceAll(s, regexp, replacement, count)
ReplaceAll returns a copy of src, replacing matches of the Regexp with the replacement text repl. Inside repl, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.
if count is less than zero, no limit on the number of replacement
let s = "'foo' 'bar'"
let s2 = replaceAll(s, "'([^']\*)'", "${1}", -1)
printf("s2=%s", s2)
// s2=foo bar
match(pattern, s)
return true if the input string s contains any match of the regular expression pattern.
use the ^ and $ modifiers to denote if the regex pattern match the full input string.
let s = "Hello"
match("^H", s) // return true since s starts with "H"
match("^h", s) // return false since s does not start with "h"
regexp(pattern, s)
this function extracts the captured "named group" matching the regular expression pattern from s.
let Email = "[email protected]"
let obj = regexp("(?P<Name>.*)@(?P<Domain>.*)", Email) // sets obj to {Name: "foo", "Domain: "@gmail.com}
let {Name, Domain} = regexp("(?P<Name>.*)@(?P<Domain>.*)", Email) // sets the var Name = "foo" and Domain = "@gmail.com"
let obj =regexp("(?P<Name>.*)@(?P<Domain>.*)", "foo") // return undefined
Updated 6 months ago