system.string

String manipulation and processing functions.

import system.string as str

Length and Access

length(s)

Get string length.

str.length("hello")   // 5
str.length("")        // 0

charAt(s, index)

Get character at index.

str.charAt("hello", 0)   // "h"
str.charAt("hello", 4)   // "o"

charCodeAt(s, index)

Get character code (ASCII/Unicode) at index.

str.charCodeAt("ABC", 0)   // 65
str.charCodeAt("hello", 0) // 104

fromCharCode(code)

Create character from code.

str.fromCharCode(65)   // "A"
str.fromCharCode(97)   // "a"

Case Conversion

upper(s)

Convert to uppercase.

str.upper("hello")   // "HELLO"
str.upper("Hello")   // "HELLO"

lower(s)

Convert to lowercase.

str.lower("HELLO")   // "hello"
str.lower("Hello")   // "hello"

capitalize(s)

Capitalize first character.

str.capitalize("hello")   // "Hello"
str.capitalize("HELLO")   // "HELLO"

title(s)

Capitalize each word.

str.title("hello world")   // "Hello World"

Search Functions

indexOf(s, substring)

Find first occurrence. Returns -1 if not found.

str.indexOf("hello", "l")    // 2
str.indexOf("hello", "lo")   // 3
str.indexOf("hello", "x")    // -1

lastIndexOf(s, substring)

Find last occurrence.

str.lastIndexOf("hello", "l")   // 3

contains(s, substring)

Check if string contains substring.

str.contains("hello", "ell")   // true
str.contains("hello", "xyz")   // false

startsWith(s, prefix)

Check if string starts with prefix.

str.startsWith("hello", "he")    // true
str.startsWith("hello", "lo")    // false

endsWith(s, suffix)

Check if string ends with suffix.

str.endsWith("hello", "lo")    // true
str.endsWith("hello", "he")    // false

Substring Extraction

substring(s, start, end)

Extract substring from start to end (exclusive).

str.substring("hello", 0, 2)   // "he"
str.substring("hello", 2, 4)   // "ll"
str.substring("hello", 3)      // "lo" (to end)

slice(s, start, end)

Extract substring with negative index support.

str.slice("hello", 1, 4)    // "ell"
str.slice("hello", -2)      // "lo"
str.slice("hello", 0, -1)   // "hell"

left(s, n)

Get first n characters.

str.left("hello", 2)   // "he"

right(s, n)

Get last n characters.

str.right("hello", 2)   // "lo"

Modification

replace(s, old, new)

Replace first occurrence.

str.replace("hello", "l", "L")   // "heLlo"

replaceAll(s, old, new)

Replace all occurrences.

str.replaceAll("hello", "l", "L")   // "heLLo"

trim(s)

Remove whitespace from both ends.

str.trim("  hello  ")   // "hello"
str.trim("\n\thello\n") // "hello"

trimLeft(s)

Remove leading whitespace.

str.trimLeft("  hello  ")   // "hello  "

trimRight(s)

Remove trailing whitespace.

str.trimRight("  hello  ")   // "  hello"

repeat(s, count)

Repeat string count times.

str.repeat("ab", 3)   // "ababab"
str.repeat("-", 10)   // "----------"

reverse(s)

Reverse string.

str.reverse("hello")   // "olleh"

Padding

padLeft(s, length, char)

Pad string on the left to reach length.

str.padLeft("5", 3, "0")      // "005"
str.padLeft("hello", 10, " ") // "     hello"

padRight(s, length, char)

Pad string on the right.

str.padRight("5", 3, "0")     // "500"
str.padRight("hello", 10)     // "hello     "

Split and Join

split(s, delimiter)

Split string into array.

str.split("a,b,c", ",")      // ["a", "b", "c"]
str.split("hello world", " ") // ["hello", "world"]
str.split("hello", "")        // ["h", "e", "l", "l", "o"]

join(array, delimiter)

Join array into string.

str.join(["a", "b", "c"], ",")    // "a,b,c"
str.join(["hello", "world"], " ") // "hello world"

lines(s)

Split string into lines.

str.lines("a\nb\nc")   // ["a", "b", "c"]

Validation

isEmpty(s)

Check if string is empty.

str.isEmpty("")       // true
str.isEmpty("hello")  // false

isBlank(s)

Check if string is empty or whitespace only.

str.isBlank("")       // true
str.isBlank("   ")    // true
str.isBlank("hello")  // false

isDigit(s)

Check if string contains only digits.

str.isDigit("123")    // true
str.isDigit("12.3")   // false
str.isDigit("12a")    // false

isAlpha(s)

Check if string contains only letters.

str.isAlpha("hello")  // true
str.isAlpha("hello1") // false

isAlphaNum(s)

Check if string contains only letters and digits.

str.isAlphaNum("hello123")  // true
str.isAlphaNum("hello_123") // false

Examples

Parse CSV Line

import system.string as str

fn parseCSV(line) {
return str.split(line, ",")
}

let data = parseCSV("Alice,30,Boston")
io.println(data[0])  // "Alice"
io.println(data[1])  // "30"
io.println(data[2])  // "Boston"

Slugify

import system.string as str

fn slugify(title) {
let s = str.lower(title)
s = str.replaceAll(s, " ", "-")
s = str.replaceAll(s, "'", "")
return s
}

io.println(slugify("Hello World"))     // "hello-world"
io.println(slugify("It's a Test"))     // "its-a-test"

Word Count

import system.string as str

fn wordCount(text) {
let words = str.split(str.trim(text), " ")
return len(words)
}

io.println(wordCount("Hello world!"))  // 2

Center Text

import system.string as str

fn center(text, width) {
let padding = width - str.length(text)
let left = padding / 2
let right = padding - left
return str.repeat(" ", left) + text + str.repeat(" ", right)
}

io.println("|" + center("Hello", 20) + "|")
// |       Hello        |

Next: system.fs →