Skip to main content

String manipulation

toLower

  • toLower(string) ⇒ string
    • returns the string in lowercase
toLower("HELLO") // return the string "hello"
toLower(" World") // return the string " world"

toUpper

  • toUpper(string) ⇒ string
    • returns the string in uppercase
toUpper("hello") // return the string "HELLO"
toUpper("wORld") // return the string "WORLD"

startsWith

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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"
indexOf(s, "b") // returns the value 1
indexOf(s, "n") // returns the value -1

subString

  • subString(s, start, end)
    • extracts substring from start to end (exclusion)
let s = "abcd"
let sub = subString(s, 1, 2) // sets the value of sub to b

parseInt

  • parseInt(s, base)
    • parse a string in the given base into a 64bit integer
    • if base is not given, it will default to 0
    • if the base argument is 0, the true base is implied by the string’s prefix (if present): 2 for "0b", 8 for "0" or "0o", 16 for "0x", and 10 otherwise
let s = "10"
parseInt(s) // return the int64 value of 10
parseInt(s, 2) // return the int64 value of 2 which is 10 in base 2

let s = "0b10"
parseInt(s) // return the int64 value of 2

parseFloat

  • parseFloat(s)
    • parse a string into a 64bit floating-point number
parseFloat("10") // return the float64 value of 10.0
parseFloat("10.11") // return the float64 value of 10.11

parseBool

  • parseBool(s)
    • returns the boolean value represented by the string
    • will return the boolean value true for 1, t, T, TRUE, true, True
    • will return the boolean value false for 0, f, F, FALSE, false, False
    • will return undefined otherwise
parseBool("1") // return the bool value true
parseBool("f") // return the bool value false
parseBool("fa") // return undefined

parseJson

  • parseJson(text)
    • parse a JSON string
    • return JsonObject, JsonArray, string, float, int, bool or null value
parseJson("[`a`, `b`, `c`]") // return a JSON array ["a", "b", "c"]
parseJson("[\"a\", \"b\", \"c\"]") // return a JSON array ["a", "b", "c"]
parseJson("{\"id\": 1, \"colA\": 3}") // return a JSON object {id: 1, colA: 3}

coalesce

  • 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

  • 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

  • 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'"
replaceAll(s, "'([^']*)'", "${1}", -1) // return the string 'foo bar'

match

  • match(pattern, s)
    • return true if the input string s contains any match of the regular expression pattern
    • use the ^ modifier to denote if the regex pattern starts with the specific character
    • use the $ modifier to denote if the regex pattern ends with the specific character
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"
match("o$", s) // return true since s ends with "o"
match("O$", s) // return false since s does not end with "O"

regexp

  • regexp(pattern, s)
    • this function extracts the captured "named group" matching the regular expression pattern from s
let Email = "foo@gmail.com"
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

sprintf

  • sprintf(format, arguments…​)
    • golang’s printf format
    • if format is not given, will default to string

template

  • template(text, variableMap)
    • golang’s printf format
    • generate text output based on input variables
    • the variableMap is a map type holding variables. or a json object
let t = `Value of a: {{.fields.a}} 
List: {{.list}}`
let opt = {"fields":{"a":"foo"}, "list": [1234, 5678]}
let s = template(t, opt)
// Value of a: foo
// List: [1234 5678]

String validation

isValidIP

  • isValidIP(s)
    • return true if the input string is a valid ip address
isValidIP("192.168.1.1") // return true
isValidIP("hello") // return false

isIPv4

  • isIPv4(s)
    • return true if the input string is a valid IPv4 address

isIPv6

  • isIPv6(s)
    • return true if the input string is a valid IPv6 address

ipNormalize

  • ipNormalize(s)
    • return normalized IP address