Synopsis
- string[] splitString(string inputString, string delimiter)
Parameters
- inputStringString to be split into separate substrings.
- delimiterDelimiter string that separates substrings. The delimiter may also be specified as a regular expression using a preceding '$' as the first character of the delimiter string. A split by '$' must be expressed by a regular expression (see example below).
Returns
Array of substrings of given input string.
Note:
Arrays can not be declared as attr or const.
Description
The splitString function splits a string into several substrings separated by a given delimiter string.
Related
Examples
Delimiter string
Input | Delimeter | CGA | Result |
---|---|---|---|
a.b.c | . | splitString("a.b.c", ".") | ["a", "b", "c"] |
.b. | . | splitString(".b.", ".") | ["", "b", ""] |
a.b↵ | \n | splitString("a.b\nc.d", "\n") | ["a.b", "c.d"] |
c.d | |||
a\b\c | \ | splitString("a\\b\\c", "\\") | ["a", "b", "c"] |
Regular expression
Input | Delimeter | CGA | Result |
---|---|---|---|
a;b;c | ; | splitString("a;b;c", "$;") | ["a", "b", "c"] |
a.b.c | \. | splitString("a.b.c", "$\\.") | ["a", "b", "c"] |
a→ → b → c | \t+ | splitString("a\t\tb\tc", "$\t+") | ["a", "b", "c"] |
a b c | \s+ | splitString("a b c", "$\\s+") | ["a", "b", "c"] |
a$b$c | \$ | splitString("a$b$c", "$\\$") | ["a", "b", "c"] |
abc | \B | splitString("abc", "$\\B") | ["a", "b", "c"] |
abacba | (?<=a)b(?=a) | splitString("abacba", "$(?<=a)b(?=a)") | ["a", "acba"] |
Parsing files
// table.csv
// a;b↵
// c;d↵
const file = readTextFile("table.csv")
rows = splitString(file, "\n")
headers = splitString(rows[0], ";")
Lot --> print(rows) // (3)[a;b,c;d,]
print(headers) // (2)[a,b]