Skip to main content

Strings and Regex

@String — string operations (96 methods)

Case transformation

ZYMBA
echo @String.toUpper("hello"); // "HELLO"
echo @String.toLower("HELLO"); // "hello"
echo @String.capitalize("hello world"); // "Hello world"
echo @String.capitalizeWords("hello world"); // "Hello World"
echo @String.foldCase("Hello"); // "hello" (Unicode-aware)

Trimming and padding

ZYMBA
echo @String.trim(" hello "); // "hello"
echo @String.trimLeft(" hello "); // "hello "
echo @String.trimRight(" hello "); // " hello"

echo @String.pad("42", 5, "0"); // "00042"

Searching

ZYMBA
$str = "Hello World";

echo @String.contains($str, "World"); // true
echo @String.containsInsensitive($str, "world"); // true
echo @String.startsWith($str, "Hello"); // true
echo @String.endsWith($str, "World"); // true
echo @String.indexOf($str, "World"); // 6
echo @String.countSubstring("abcabc", "abc"); // 2

Extracting

ZYMBA
$str = "Hello, World!";

echo @String.slice($str, 7, 5); // "World"
echo @String.charAt($str, 0); // "H"
echo @String.head($str); // "Hello, World" (all but last char)
echo @String.tail($str); // "ello, World!" (all but first char)

Replacing and splitting

ZYMBA
echo @String.replace("Hello World", "World", "Zymba");
// "Hello Zymba"

$parts = @String.split("a,b,c", ",");
// ["a", "b", "c"]

$parts = @String.splitNonEmpty("a,,b,,c", ",");
// ["a", "b", "c"] (skips empty strings)

Comparison

ZYMBA
echo @String.equals("abc", "abc"); // true
echo @String.equalsInsensitive("ABC", "abc"); // true
echo @String.compare("a", "b"); // -1
echo @String.distanceBetween("kitten", "sitting"); // 3 (Levenshtein)

Measurement

ZYMBA
echo @String.countChars("Hello"); // 5 (Unicode characters)
echo @String.countBytes("Hello"); // 5 (bytes)
echo @String.isEmpty(""); // true

Encoding

ZYMBA
echo @String.toHex("AB"); // "4142"
echo @String.fromHex("4142"); // "AB"
echo @String.escapeC("line1\nline2"); // "line1\\nline2"

Validation

ZYMBA
echo @String.isPrintable("Hello"); // true
echo @String.isWhiteSpace(" \t"); // true
echo @String.isDecimalDigits("12345"); // true
echo @String.isHexDigits("1a2f"); // true
echo @String.isBinaryDigits("10110"); // true
echo @String.isValidUTF8("Hello"); // true
echo @String.isValidASCII("Hello"); // true
echo @String.isSingleChar("A"); // true
ZYMBA
$str = "Hello World Hello";
echo @String.lastIndexOf($str, "Hello"); // 12
echo @String.lastIndexOfInsensitive($str, "hello"); // 12
echo @String.indexOfInsensitive($str, "world"); // 6

Modification

ZYMBA
echo @String.insert("HelloWorld", " ", 5); // "Hello World"
echo @String.splice("Hello World", 6, 5, "Zymba"); // "Hello Zymba"
echo @String.truncate("Hello World", 0, 8, "..."); // "Hello..."
echo @String.normalizeLineBreaks("a\r\nb\rc"); // "a\nb\nc"
echo @String.normalizeWhiteSpace(" too many "); // "too many"
echo @String.quote("Hello", "'"); // "'Hello'"

Other utilities

ZYMBA
echo @String.reverse("Hello"); // "olleH"
echo @String.repeat("ab", 3); // "ababab"
echo @String.concat("a", "b", "c"); // "abc"
echo @String.format("Hello, %s! You are %d.", "Alice", 30);
// "Hello, Alice! You are 30."
echo @String.soundex("Robert"); // "R163"
echo @String.metaphone("Robert"); // "RBRT"
echo @String.crc32("test"); // CRC32 checksum

@Regex — regular expressions (11 methods)

Testing

ZYMBA
echo @Regex.test("hello123", "/[0-9]+/"); // true
echo @Regex.test("hello", "/^[a-z]+$/"); // true
echo @Regex.isValidPattern("/[a-z/"); // false (invalid)

Matching

ZYMBA
// First match with groups
$m = @Regex.match("hello123world", "/([a-z]+)([0-9]+)/");
// ["hello123", "hello", "123"]

// All matches
$all = @Regex.matchAll("a1 b2 c3", "/([a-z])([0-9])/");
// [["a1","a","1"], ["b2","b","2"], ["c3","c","3"]]

// Count matches
echo @Regex.count("a1 b2 c3", "/[0-9]/"); // 3

Replacing

ZYMBA
echo @Regex.replace("Hello World 123", "/[0-9]+/", "###");
// "Hello World ###"

// With backreferences
echo @Regex.replace("John Doe", "/(\w+) (\w+)/", "$2, $1");
// "Doe, John"

// Multiple replacements
$result = @Regex.replaceMultiple($str, [
"/\\s+/": " ",
"/[^a-z0-9 ]/i": ""
]);

// With callback
$result = @Regex.substitute("hello world", "/[a-z]+/", function($match) {
return @String.toUpper($match[0]);
});
// "HELLO WORLD"

Splitting

ZYMBA
$parts = @Regex.split("one, two, three", "/,\\s*/");
// ["one", "two", "three"]