String
Library of functions for the string type.
Functions
Base64
Returns the base64 encoding of a string
String.Base64(string: string)
Parameters
string
: The string to encode as base64.
Returns
string
: The base64 encoded string.
String.Base64("Hello World!")
// Result:
// "SGVsbG8gV29ybGQh"
Capitalize
Makes the first character of a string be uppercase and the rest lowercase.
String.Capitalize(string: string)
Parameters
string
: The string to be capitalized.
Returns
string
: The capitalized string.
String.Capitalize("SNAPI")
// Result:
// "Snapi"
String.Capitalize("snapi")
// Result:
// "Snapi"
String.Capitalize("snAPI")
// Result:
// "Snapi"
String.Capitalize("Snapi")
// Result:
// "Snapi"
Contains
Returns true if a given string contains another given string.
String.Contains(s1: string, s2: string)
Parameters
-
s1
: The input string to search within. -
s2
: The substring to search for within s1.
Returns
bool
: True if s1 contains s2, false otherwise.
String.Contains("Snapi", "api")
// Result:
// true
String.Contains("Snapi", "API")
// Result:
// false
CountSubString
Count the number of occurrences of a substring in a string.
String.CountSubString(string: string, substring: string)
Parameters
-
string
: The string on which the occurrences are counted. -
substring
: The substring which is counted.
Returns
int
: The number of occurrences.
String.CountSubString("aXbX", "X")
// Result:
// 2
String.CountSubString("aXbX", "y")
// Result:
// 0
String.CountSubString("aXbX", "aX")
// Result:
// 1
Decode
Builds a string from a binary, given an encoding.
String.Decode(string: string, encoding: string)
Parameters
-
string
: The string to decode. -
encoding
: The encoding that the binary value uses.
Returns
string
: The decoded string.
let
b = String.Encode("Hello world", "utf-8")
in
String.Decode(b, "utf-8")
// Result:
// "Hello world"
Empty
Returns true the string is empty.
String.Empty(string: string)
Parameters
string
: The string to check if empty.
Returns
bool
: True if the string is empty, false otherwise.
String.Empty("")
// Result:
// true
String.Empty("Hello!")
// Result:
// false
Encode
Converts a string to a binary, given an encoding.
String.Encode(string: string, encoding: string)
Parameters
-
string
: The string to encode. -
encoding
: The encoding to use.
Returns
binary
: The encoded binary.
String.EncodeString("Hello world", "utf-8"))
From
Builds a string from a number, bool, temporal or location.
String.From(value: number or bool or temporal)
Parameters
value
: The value to convert to string.
Returns
string
: The string representation of the value.
String.From(123)
// Result:
// "123"
String.From(true)
// Result:
// "true"
String.From(Date.Build(1975, 6, 23))
// Result:
// "1975-06-23"
LTrim
Removes white space characters from the beginning of a string.
String.LTrim(string: string)
Parameters
string
: The string to be trimmed.
Returns
string
: The trimmed string.
String.LTrim(" abc ")
// Result:
// "abc"
String.LTrim("abc ")
// Result:
// "abc "
String.LTrim(null)
// Result:
// null
Length
Compute the size of a string.
String.Length(string: string)
Parameters
string
: The string to compute the size.
String.Length("Hello John")
// Result:
// 10
LevenshteinDistance
Calculates the Levenshtein distance between two strings.
String.LevenshteinDistance(string1: string, string2: string)
Parameters
-
string1
: The string from which the levenshtein distance is calculated. -
string2
: The string to which the levenshtein distance is calculated.
Returns
int
: The levenshtein distance between the two strings.
String.LevenshteinDistance("Hello world", "Hello warld")
// Result:
// 1
String.LevenshteinDistance("Hello world", "Hello John")
// Result:
// 4
The Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other.
Lower
Convert a string to lowercase.
String.Lower(string: string)
Parameters
string
: The string to convert to lowercase.
Returns
string
: The converted string.
lower("abC")
// Result:
// "abc"
lower(null)
// Result:
// null
RTrim
Removes white space characters from the end of a string.
String.RTrim(string: string)
Parameters
string
: The string to be trimmed.
Returns
string
: The trimmed string.
String.RTrim(" abc ")
// Result:
// " abc"
String.RTrim(" abc")
// Result:
// " abc"
Read
Reads the contents of a location as a string.
String.Read(location: location)
Parameters
location
: The location to read.
Returns
string
: The contents of the location as a string.
String.Read("file:///tmp/test.txt")
ReadLines
Reads the contents of a location as lines of text
String.ReadLines(location: location, encoding: optional string)
Parameters
-
location
: The location to read lines from. -
encoding
: Specifies the encoding of the data; if not specified defaults to utf-8.
Returns
collection(string)
: The lines of text in the location.
String.ReadLines("http://somewhere/data.log")"
Replace
Replace all matches of substring by a new string.
String.Replace(string: string, pattern: string, newSubString: string)
Parameters
-
string
: The string to be analyzed. -
pattern
: The substring to match. -
newSubString
: The new substring to replace matches with.
Returns
string
: The string with all matches replaced.
String.Replace("Hello John", "John", "Jane")
// Result:
// "Hello Jane"
String.Replace("Hello John", "o", "+")
// Result:
// "Hell+ J+hn!"
Replicate
Replicates a string by coping the substring for the specific number of repetitions.
String.Replicate(string: string, repetitions: int)
Parameters
-
string
: The string to replicate. -
repetitions
: The number of repetitions.
Returns
string
: The replicated string.
String.Replicate("x", 4)
// Result:
// "xxxx"
String.Replicate("abc,", 2)
// Result:
// "abc,abc,"
Reverse
Reverses a string.
String.Reverse(string: string)
Parameters
string
: The string to reverse.
Returns
string
: The reversed string.
String.Reverse("1234")
// Result:
// "4321"
Split
Split a string into a list of strings given a separator.
String.Split(string: string, separator: string)
Parameters
-
string
: The string to split. -
separator
: The separator by which the string is split.
Returns
list(string)
: The list of strings.
String.Split("Value1||Value2", "||")
// Result:
// ["Value1","Value2"]
String.Split("Value1|Value2", "|")
// Result:
// ["Value1","Value2"]
StartsWith
Check if a string starts with a given prefix.
String.StartsWith(string: string, prefix: string)
Parameters
-
string
: The string to find the prefix. -
prefix
: The prefix to match.
Returns
bool
: True if the beginning of a string matches the prefix, false otherwise.
String.StartsWith("Hello world", "Hello")
// Result:
// true
String.StartsWith("Hello world", "world")
// Result:
// false
SubString
Extract a substring from a string, given the start index and length of extraction.
String.SubString(string: string, index: int, size: int)
Parameters
-
string
: The string of which a substring is extracted. -
index
: The start position to extract from. -
size
: The size of the substring to extract.
Returns
string
: The extracted substring.
String.SubString("Hello John", 4, 2)
// Result:
// "lo"
String.SubString("Hello John", 7, -1)
// Result:
// "John"
The index starts at 1. A negative number as the length means the remainder of the string.
Trim
Removes white space characters from the beginning and end of a string.
String.Trim(string: string)
Parameters
string
: The string to be trimmed.
Returns
string
: The trimmed string.
String.Trim(" abc ")
// Result:
// "abc"
String.Trim("abc ")
// Result:
// "abc"
Upper
Convert a string to uppercase.
String.Upper(string: string)
Parameters
string
: The string to convert to uppercase.
Returns
string
: The converted string.
String.Upper("abC") // "ABC"
// Result:
// "ABC"
String.Upper(null) // null
// Result:
// null