Strings
Strings in Chuks are immutable sequences of characters. They support Unicode (UTF-8) and come with a comprehensive set of built-in methods for manipulation and searching — no imports needed.
Declaration
Section titled “Declaration”var greeting: string = "Hello, World!";var name = "Alice"; // Type inferred as stringconst msg = "Immutable string";Properties
Section titled “Properties”length: int
Section titled “length: int”Returns the number of characters in the string. This is a property, not a method — access it without parentheses.
var s = "Hello";println(s.length); // 5Accessing Characters
Section titled “Accessing Characters”at(index: int): string
Section titled “at(index: int): string”Returns the character at the given index. Supports negative indexing (-1 returns the last character).
var s = "Hello";println(s.at(0)); // "H"println(s.at(-1)); // "o"charAt(index: int): string
Section titled “charAt(index: int): string”Returns the character at the given index. Returns an empty string if out of range.
var s = "Hello";println(s.charAt(1)); // "e"charCodeAt(index: int): int
Section titled “charCodeAt(index: int): int”Returns the Unicode code point of the character at the given index.
var s = "ABC";println(s.charCodeAt(0)); // 65strings.fromCharCode(code: int): string
Section titled “strings.fromCharCode(code: int): string”Converts an integer character code to a single-character string. It is the inverse of charCodeAt.
import { strings } from "std/strings"
println(strings.fromCharCode(65)); // Aprintln(strings.fromCharCode(97)); // aprintln(strings.fromCharCode(48)); // 0println(strings.fromCharCode(33)); // !Use it to build strings character by character:
import { strings } from "std/strings"
// Build "Hi!" from char codesvar result: string = "";result = result + strings.fromCharCode(72); // Hresult = result + strings.fromCharCode(105); // iresult = result + strings.fromCharCode(33); // !println(result); // Hi!Generate sequences like the alphabet:
import { strings } from "std/strings"
var alphabet: string = "";for (var i: int = 0; i < 26; i = i + 1) { alphabet = alphabet + strings.fromCharCode(65 + i);}println(alphabet); // ABCDEFGHIJKLMNOPQRSTUVWXYZSearching
Section titled “Searching”indexOf(search: string, from?: int): int
Section titled “indexOf(search: string, from?: int): int”Returns the index of the first occurrence of the search string, or -1 if not found.
var s = "Hello World";println(s.indexOf("World")); // 6println(s.indexOf("xyz")); // -1lastIndexOf(search: string, from?: int): int
Section titled “lastIndexOf(search: string, from?: int): int”Returns the index of the last occurrence of the search string, or -1.
var s = "abcabc";println(s.lastIndexOf("abc")); // 3includes(search: string, from?: int): bool
Section titled “includes(search: string, from?: int): bool”Returns true if the string contains the search string.
var s = "Hello World";println(s.includes("World")); // trueprintln(s.includes("xyz")); // falsestartsWith(search: string, position?: int): bool
Section titled “startsWith(search: string, position?: int): bool”Returns true if the string starts with the given search string.
var s = "Hello World";println(s.startsWith("Hello")); // trueprintln(s.startsWith("World")); // falseendsWith(search: string, length?: int): bool
Section titled “endsWith(search: string, length?: int): bool”Returns true if the string ends with the given search string.
var s = "Hello World";println(s.endsWith("World")); // trueprintln(s.endsWith("Hello")); // falsesearch(pattern: string): int
Section titled “search(pattern: string): int”Returns the index of the first match against the pattern, or -1.
var s = "Hello World";println(s.search("World")); // 6match(pattern: string): []string
Section titled “match(pattern: string): []string”Returns an array of matches for the given pattern. Returns an empty array if no match.
var s = "Hello World";var result = s.match("World");println(result); // ["World"]matchAll(pattern: string): []string
Section titled “matchAll(pattern: string): []string”Returns an array of all matches for the given pattern.
var s = "ababab";var result = s.matchAll("ab");println(result); // ["ab", "ab", "ab"]Combining & Padding
Section titled “Combining & Padding”concat(...strings: string): string
Section titled “concat(...strings: string): string”Concatenates the given strings to this string and returns the result.
var s = "Hello";println(s.concat(" ", "World")); // "Hello World"padStart(targetLength: int, padString?: string): string
Section titled “padStart(targetLength: int, padString?: string): string”Pads the start of the string until it reaches the target length. Default pad string is a space.
var s = "5";println(s.padStart(3, "0")); // "005"padEnd(targetLength: int, padString?: string): string
Section titled “padEnd(targetLength: int, padString?: string): string”Pads the end of the string until it reaches the target length.
var s = "5";println(s.padEnd(3, "0")); // "500"repeat(count: int): string
Section titled “repeat(count: int): string”Returns the string repeated the specified number of times.
var s = "abc";println(s.repeat(3)); // "abcabcabc"Replacing
Section titled “Replacing”replace(search: string, replacement: string): string
Section titled “replace(search: string, replacement: string): string”Replaces the first occurrence of the search string with the replacement.
var s = "Hello World";println(s.replace("World", "Chuks")); // "Hello Chuks"replaceAll(search: string, replacement: string): string
Section titled “replaceAll(search: string, replacement: string): string”Replaces all occurrences of the search string with the replacement.
var s = "aabaa";println(s.replaceAll("a", "x")); // "xxbxx"Extracting
Section titled “Extracting”slice(start: int, end?: int): string
Section titled “slice(start: int, end?: int): string”Extracts a section of the string and returns a new string. Supports negative indexes.
var s = "Hello World";println(s.slice(0, 5)); // "Hello"println(s.slice(-5)); // "World"substring(start: int, end?: int): string
Section titled “substring(start: int, end?: int): string”Returns the part of the string between the start and end indexes.
var s = "Hello World";println(s.substring(6, 11)); // "World"split(separator: string, limit?: int): []string
Section titled “split(separator: string, limit?: int): []string”Splits the string into an array of substrings using the given separator.
var s = "a,b,c,d";var parts = s.split(",");println(parts); // ["a", "b", "c", "d"]Trimming
Section titled “Trimming”trim(): string
Section titled “trim(): string”Removes whitespace from both ends of the string.
var s = " Hello ";println(s.trim()); // "Hello"trimStart(): string
Section titled “trimStart(): string”Removes whitespace from the start of the string.
var s = " Hello ";println(s.trimStart()); // "Hello "trimEnd(): string
Section titled “trimEnd(): string”Removes whitespace from the end of the string.
var s = " Hello ";println(s.trimEnd()); // " Hello"Case Conversion
Section titled “Case Conversion”toLowerCase(): string
Section titled “toLowerCase(): string”Returns the string converted to lowercase.
var s = "Hello World";println(s.toLowerCase()); // "hello world"toUpperCase(): string
Section titled “toUpperCase(): string”Returns the string converted to uppercase.
var s = "Hello World";println(s.toUpperCase()); // "HELLO WORLD"toLocaleLowerCase(): string
Section titled “toLocaleLowerCase(): string”Locale-specific lowercase conversion. Equivalent to toLowerCase() in the current implementation.
toLocaleUpperCase(): string
Section titled “toLocaleUpperCase(): string”Locale-specific uppercase conversion. Equivalent to toUpperCase() in the current implementation.
std/strings Module
Section titled “std/strings Module”In addition to built-in string methods, the std/strings module provides standalone utility functions that take a string as the first argument:
import { strings } from "std/strings";| Function | Description |
|---|---|
strings.split(s, sep) | Split string into array |
strings.join(arr, sep) | Join array into string |
strings.replace(s, old, new) | Replace all occurrences |
strings.trim(s) | Trim whitespace from both ends |
strings.trimLeft(s) | Trim whitespace from the left |
strings.trimRight(s) | Trim whitespace from the right |
strings.toLower(s) | Convert to lowercase |
strings.toUpper(s) | Convert to uppercase |
strings.contains(s, substr) | Check if string contains substring |
strings.indexOf(s, substr) | Index of first occurrence (-1 if not found) |
strings.lastIndexOf(s, substr) | Index of last occurrence (-1 if not found) |
strings.substring(s, start, end?) | Extract substring by index range |
strings.startsWith(s, prefix) | Check if string starts with prefix |
strings.endsWith(s, suffix) | Check if string ends with suffix |
strings.repeat(s, count) | Repeat string N times |
strings.charAt(s, index) | Get character at index |
import { strings } from "std/strings";
const parts = strings.split("a,b,c", ",");const joined = strings.join(parts, "-"); // "a-b-c"const replaced = strings.replace("hello world", "world", "chuks"); // "hello chuks"
// Additional utility functionsconst trimmed = strings.trim(" hello "); // "hello"const upper = strings.toUpper("hello"); // "HELLO"const lower = strings.toLower("HELLO"); // "hello"
println(strings.contains("hello world", "world")); // trueprintln(strings.startsWith("hello", "he")); // trueprintln(strings.indexOf("abcabc", "bc")); // 1println(strings.repeat("ab", 3)); // "ababab"println(strings.charAt("hello", 0)); // "h"