Skip to content
Draft

strings

The strings module provides advanced string manipulation beyond built-in methods.

import strings
import { trim, split, join } from strings

Remove whitespace from both ends:

@strings#trim(" hello ") @// "hello"

Remove whitespace from start:

@strings#trim_start(" hello ") @// "hello "

Remove whitespace from end:

@strings#trim_end(" hello ") @// " hello"

Remove specific characters:

@strings#trim_chars("...hello...", ".") @// "hello"

Split by delimiter:

@strings#split("a,b,c", ",") @// ["a", "b", "c"]

Split into at most n parts:

@strings#split_n("a,b,c,d", ",", 2) @// ["a", "b,c,d"]

Split by line breaks:

@strings#split_lines("line1\nline2\nline3")

Split by any whitespace:

@strings#split_whitespace("a b c") @// ["a", "b", "c"]

Join strings with delimiter:

@strings#join(["a", "b", "c"], ",") @// "a,b,c"

Concatenate strings:

@strings#concat("hello", " ", "world") @// "hello world"

Convert to uppercase:

@strings#to_upper("hello") @// "HELLO"

Convert to lowercase:

@strings#to_lower("HELLO") @// "hello"

Convert to title case:

@strings#to_title("hello world") @// "Hello World"

Convert to camelCase:

@strings#to_camel("hello world") @// "helloWorld"

Convert to PascalCase:

@strings#to_pascal("hello world") @// "HelloWorld"

Convert to snake_case:

@strings#to_snake("helloWorld") @// "hello_world"

Convert to kebab-case:

@strings#to_kebab("helloWorld") @// "hello-world"

Check if string contains substring:

@strings#contains("hello world", "world") @// true

Check prefix:

@strings#starts_with("hello world", "hello") @// true

Check suffix:

@strings#ends_with("hello world", "world") @// true

Find first occurrence:

@strings#find("hello world", "world") @// 6

Find last occurrence:

@strings#find_last("hello hello", "hello") @// 6

Count occurrences:

@strings#count("hello hello", "hello") @// 2

Replace all occurrences:

@strings#replace("hello world", "world", "draft") @// "hello draft"

Replace first n occurrences:

@strings#replace_n("hello hello", "hello", "hi", 1) @// "hi hello"

Replace range:

@strings#replace_range("hello world", 6, 11, "draft") @// "hello draft"

Pad at start to reach length:

@strings#pad_start("hello", 10) @// " hello"
@strings#pad_start("hello", 10, "0") @// "00000hello"

Pad at end to reach length:

@strings#pad_end("hello", 10) @// "hello "
@strings#pad_end("hello", 10, "0") @// "hello00000"

Extract substring:

@strings#substring("hello world", 0, 5) @// "hello"

Slice with range:

@strings#slice("hello world", 6) @// "world"
@strings#slice("hello world", 0, 5) @// "hello"

Get leftmost characters:

@strings#left("hello", 3) @// "hel"

Get rightmost characters:

@strings#right("hello", 3) @// "llo"

Reverse string:

@strings#reverse("hello") @// "olleh"

Reverse word order:

@strings#reverse_words("hello world") @// "world hello"

Get character array:

@strings#chars("hello") @// ['h', 'e', 'l', 'l', 'o']

Get string length:

@strings#length("hello") @// 5

Check if empty:

@strings#is_empty("") @// true

Check if all alphabetic:

@strings#is_alpha("hello") @// true

Check if all numeric:

@strings#is_numeric("12345") @// true

Check if alphanumeric:

@strings#is_alphanumeric("abc123") @// true

Check if all whitespace:

@strings#is_whitespace(" ") @// true

Validate email format:

@strings#is_email("user@example.com") @// true

Validate URL format:

@strings#is_url("https://example.com") @// true

Validate IPv4 address:

@strings#is_ipv4("192.168.1.1") @// true

Encode to Base64:

@strings#base64_encode("hello") @// "aGVsbG8="

Decode from Base64:

@strings#base64_decode("aGVsbG8=") @// "hello"

URL encode:

@strings#url_encode("hello world") @// "hello%20world"

URL decode:

@strings#url_decode("hello%20world") @// "hello world"

HTML entity encode:

@strings#html_encode("<div>") @// "&lt;div&gt;"

HTML entity decode:

@strings#html_decode("&lt;div&gt;") @// "<div>"

MD5 hash:

@strings#md5("hello") @// "5d41402abc4b2a76b9719d911017c592"

SHA-1 hash:

@strings#sha1("hello") @// "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d"

SHA-256 hash:

@strings#sha256("hello") @// "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"

Format string with arguments:

@strings#format("Hello, {0}!", "World") @// "Hello, World!"

Template with named placeholders:

@strings#template("Hello, {name}!", {name: "World"}) @// "Hello, World!"

Indent each line:

@strings#indent("hello\nworld", 4) @// " hello\n world"

Remove common indentation:

@strings#dedent(" hello\n world") @// "hello\nworld"

Wrap text at width:

@strings#wrap("hello world this is a test", 10)
@// "hello\nworld this\nis a test"

Truncate with ellipsis:

@strings#truncate("hello world", 8) @// "hello..."

Match with wildcards:

@strings#wildcard_match("hello.txt", "*.txt") @// true

Regular expression match:

@strings#regex_match("hello123", r"\w+\d+") @// true

Replace with regex:

@strings#regex_replace("hello123", r"\d+", "") @// "hello"

Find all regex matches:

@strings#regex_find_all("a1b2c3", r"\d") @// ["1", "2", "3"]