Predefined functions for string
parseNumber
Converts a string to a number.
Parameters
None
Returns
number - the integer part of a number by removing any fractional digits.
Examples
node some_node { do { #log("0.75e-1".parseNumber()); // 0.075 #log("1E+3".parseNumber()); // 1000 #log("10.289".parseNumber()); // 10.289 #log("10".parseNumber()); // 10 #log("X)".parseNumber().isNaN()); // true } }
length
Returns the length of a string.
Parameters
None
Returns
number - the length of a string.
Examples
node some_node { do { #log("".length()); // 0 #log("123456".length()); // 6 } }
repeat
Returns a new string which contains the repeatCount number of copies of the string.
If repeatCount is 0, the empty string is returned.
Note:
- negative value of
repeatCountparameter will cause compile error
Parameters
| Name | Type | Description |
|---|---|---|
| repeatCount | number | number of copies of the string |
Returns
string - the repeated string
Examples
node some_node { do { #log("s".repeat(0)); // "" #log("s".repeat(4)); // "ssss" } }
replaceFirst
Returns a new string with first found entry of searchString being replaced by replaceString.
The source string stays unmodified.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the substring that is to be replaced by replaceString |
| replaceString | string | the string that replaces the substring specified by the searchString |
Returns
string - a new string with first found entry of searchString being replaced by replaceString.
Examples
node some_node { do { var str = "aaaa"; #log(str.replaceFirst("aa", "b")); // "baa" #log(str); // "aaaa" } }
replaceLast
Returns a new string with last found entry of searchString being replaced by replaceString.
The source string stays unmodified.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the substring that is to be replaced by replaceString |
| replaceString | string | the string that replaces the substring specified by the searchString |
Returns
string - a new string with last found entry of searchString being replaced by replaceString.
Examples
node some_node { do { var str = "aaaa"; #log(str.replaceLast("aa", "b")); // "aab" #log(str); // "aaaa" } }
replaceAll
Returns a new string with all found entries of searchString being replaced by replaceString.
The source string stays unmodified.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the substring that is to be replaced by replaceString |
| replaceString | string | the string that replaces the substring specified by the searchString |
Returns
string - a new string with all found entries of searchString being replaced by replaceString.
Examples
node some_node { do { var str = "aaaa"; #log(str.replaceAll("aa", "b")); // "bb" #log(str); // "aaaa" } }
reverse
Returns a new string with chars of the source string in reversed order.
Parameters
None
Returns
string - a new string with chars of the source string in reversed order.
Examples
node some_node { do { var str = "abc"; #log(str.reverse()); // "cba" #log(str); // "abc" } }
slice
Returns a copy of a section extracted from the string. Input parameters start and end define the section of the string as the range [start,end).
A negative value of an index (start or end) means an offset from the end of the array (please, see the examples).
If start is omitted than slice will extract elements starting from the index 0.
If end is omitted or end >= array.length() than slice will extract elements through to the end of array.
Note:
- the float value of an index will be truncated to integer value.
Parameters
| Name | Type | Description |
|---|---|---|
| start | number? | integer index at which to start the extraction |
| end | number? | integer index at which to end the extraction (not including) |
Returns
string - a new string containing the extracted elements.
Examples
node some_node { do { var str = "123456"; #log(str.slice()); // "123456" #log(str.slice(2,4)); // "34" #log(str.slice(2)); // "3456" #log(str.slice(-2)); // "56" // 2.9 is truncated to 2 #log(str.slice(2.9)); // "3456" #log(str.slice(2,10)); // "3456" } }
toLowerCase
Return a new string with all the alphabetic chars being converted to lowercase.
The source string stays unmodified.
Parameters
None
Returns
string - a new string with all the alphabetic chars being converted to lowercase.
Examples
node some_node { do { var str = "AA aa"; #log(str.toLowerCase()); // "aa aa" #log(str); // "AA aa" } }
toUpperCase
Return a new string with all the alphabetic chars being converted to uppercase.
The source string stays unmodified.
Parameters
None
Returns
string - a new string with all the alphabetic chars being converted to uppercase.
Examples
node some_node { do { var str = "AA aa"; #log(str.toUpperCase()); // "AA AA" #log(str); // "AA aa" } }
trim
Returns a new string with leading and trailing whitespaces being removed.
The source string stays unmodified.
Parameters
None
Returns
string - a new string with leading and trailing whitespaces being removed.
Examples
node some_node { do { var str = " sss "; #log(str.trim()); // "sss" #log(str); // " sss " } }
trimEnd
Returns a new string with trailing whitespaces being removed.
The source string stays unmodified.
Parameters
None
Returns
string - a new string with trailing whitespaces being removed.
Examples
node some_node { do { var str = " sss "; #log(str.trimEnd()); // " sss" #log(str); // " sss " } }
trimStart
Returns a new string with leading whitespaces being removed.
The source string stays unmodified.
Parameters
None
Returns
string - a new string with leading whitespaces being removed.
Examples
node some_node { do { var str = " sss "; #log(str.trimStart()); // "sss " #log(str); // " sss " } }
concat
Returns a new string with elements of values being concatenated with the value of a string.
Parameters
| Name | Type | Description |
|---|---|---|
| values | string[] | strings being concatenated with the calling string |
Returns
string - a new string with elements of values being concatenated with the value of a string.
Examples
node some_node { do { var str = "hello?"; #log(str.concat([" is", " there", " anybody", " in", " there?"])); // "hello? is there anybody in there?" } }
startsWith
Returns true if a string sliced from the position begins with the characters of a searchString parameter, false otherwise.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the characters to be searched for in a string |
| position | number? | the index at which to start the search |
Returns
true if a string sliced from position to the end begins with the characters of a searchString parameter, false otherwise.
Examples
node some_node { do { var str = "Just nod if you can hear me"; #log(str.startsWith("Just")); // true #log(str.startsWith("nod")); // false #log(str.startsWith("nod", 5)); // true } }
includes
Returns true if there is an entry of searchString in a string sliced from the position, false otherwise.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the characters to be searched for in a string |
| position | number? | the index at which to start the search |
Returns
true if there is an entry of searchString in a string sliced from the position to the end, false otherwise.
Examples
node some_node { do { var str = "Just nod if you can hear me"; #log(str.includes("you")); // true #log(str.includes("you", -15)); // false #log(str.includes("you", 15)); // true } }
endsWith
Returns true if a string sliced from the position begins with the characters of a searchString parameter, false otherwise.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the characters to be searched for in a string |
| position | number? | the index at which to start the search |
Returns
true if there is an entry of searchString in a string sliced from the position to the end, false otherwise.
Examples
node some_node { do { var str = "Just nod if you can hear me"; #log(str.endsWith("me")); // true #log(str.endsWith("nod")); // false } }
split
Splits a string into an ordered list of substrings, puts these substrings into an array, and returns the array.
If separator contains multiple characters, that entire character sequence must be found in order to split.
If separator appears at the beginning (or end) of the string, it still has the effect of splitting. The result is an empty (i.e. zero length) string, which appears at the first (or last) position of the returned array.
If separator is an empty string (""), the string is converted to an array of each of its UTF-16 "characters".
If limit is omitted than the string will be divided as many times as possible.
Parameters
| Name | Type | Description |
|---|---|---|
| separator | string | the pattern describing where each split should occur |
| limit | number? | a non-negative integer specifying a limit on the number of substrings to be included in the array |
Returns
string[] - array of separated substrings
Examples
node some_node { do { var str = "Is there anyone at home?"; #log(str.split("not_found")); // ["Is there anyone at home?"] #log(str.split(" ")); // ["Is","there","anyone","at","home?"] } }
indexOf
Returns the position of the first occurrence of a substring. Returns -1 if the value is not found.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the characters to be searched for in a string |
| position | number? | the index at which to start the search |
Returns
number the position of the first occurrence of a substring, -1 if the value is not found.
Examples
node some_node { do { var str = "012345674"; #log(str.indexOf("not_found")); // -1 #log(str.indexOf("4")); // 4 #log(str.indexOf("0", 1)); // -1 } }
lastIndexOf
Returns the position of the last occurrence of a substring. Returns -1 if the value is not found.
Parameters
| Name | Type | Description |
|---|---|---|
| searchString | string | the characters to be searched for in a string |
| position | number? | the index of the last character in the string to be considered as the beginning of a match |
Returns
number - the position of the last occurrence of a substring, -1 if the value is not found.
Examples
node some_node { do { var str = "012345674"; #log(str.lastIndexOf("not_found")); // -1 #log(str.lastIndexOf("4")); // 8 #log(str.lastIndexOf("1", 0)); // -1 } }
parseJSON
Parse a string as JSON.
Parameters
None
Returns
unknown? - value or object corresponding to the given JSON text, or null if the string contains invalid JSON syntax.
Note:
- The return type is
unknown?because the structure depends on the parsed JSON content - You can use type assertions to work with the returned value in a type-safe manner
Examples
node some_node { do { // Parsing a valid JSON object var jsonString = #stringify({ a: 3, b: "foo" }); var parsedObject = jsonString.parseJSON() as { a: number; b: string; }; #log(parsedObject?.a); // 3 #log(parsedObject?.b); // "foo" var invalidJson = "[invalidJSON"; #log(invalidJson.parseJSON() is null); // true } }