Skip to content

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.

var greeting: string = "Hello, World!";
var name = "Alice"; // Type inferred as string
const msg = "Immutable string";

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); // 5

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"

Returns the character at the given index. Returns an empty string if out of range.

var s = "Hello";
println(s.charAt(1)); // "e"

Returns the Unicode code point of the character at the given index.

var s = "ABC";
println(s.charCodeAt(0)); // 65

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)); // A
println(strings.fromCharCode(97)); // a
println(strings.fromCharCode(48)); // 0
println(strings.fromCharCode(33)); // !

Use it to build strings character by character:

import { strings } from "std/strings"
// Build "Hi!" from char codes
var result: string = "";
result = result + strings.fromCharCode(72); // H
result = result + strings.fromCharCode(105); // i
result = 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); // ABCDEFGHIJKLMNOPQRSTUVWXYZ

Returns the index of the first occurrence of the search string, or -1 if not found.

var s = "Hello World";
println(s.indexOf("World")); // 6
println(s.indexOf("xyz")); // -1

lastIndexOf(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")); // 3

includes(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")); // true
println(s.includes("xyz")); // false

startsWith(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")); // true
println(s.startsWith("World")); // false

endsWith(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")); // true
println(s.endsWith("Hello")); // false

Returns the index of the first match against the pattern, or -1.

var s = "Hello World";
println(s.search("World")); // 6

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"]

Returns an array of all matches for the given pattern.

var s = "ababab";
var result = s.matchAll("ab");
println(result); // ["ab", "ab", "ab"]

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"

Returns the string repeated the specified number of times.

var s = "abc";
println(s.repeat(3)); // "abcabcabc"

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"

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"

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"]

Removes whitespace from both ends of the string.

var s = " Hello ";
println(s.trim()); // "Hello"

Removes whitespace from the start of the string.

var s = " Hello ";
println(s.trimStart()); // "Hello "

Removes whitespace from the end of the string.

var s = " Hello ";
println(s.trimEnd()); // " Hello"

Returns the string converted to lowercase.

var s = "Hello World";
println(s.toLowerCase()); // "hello world"

Returns the string converted to uppercase.

var s = "Hello World";
println(s.toUpperCase()); // "HELLO WORLD"

Locale-specific lowercase conversion. Equivalent to toLowerCase() in the current implementation.

Locale-specific uppercase conversion. Equivalent to toUpperCase() in the current implementation.

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";
FunctionDescription
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 functions
const trimmed = strings.trim(" hello "); // "hello"
const upper = strings.toUpper("hello"); // "HELLO"
const lower = strings.toLower("HELLO"); // "hello"
println(strings.contains("hello world", "world")); // true
println(strings.startsWith("hello", "he")); // true
println(strings.indexOf("abcabc", "bc")); // 1
println(strings.repeat("ab", 3)); // "ababab"
println(strings.charAt("hello", 0)); // "h"