Text Module

The Language::Text module provides string utilities and regular expression pattern matching.

xxml
#import Language::Text;

StringUtils

Static utility class for string manipulation operations.

Trimming and Case

MethodParametersReturnsDescription
trimstr: String^String^Remove leading/trailing whitespace
toLowerCasestr: String^String^Convert to lowercase
toUpperCasestr: String^String^Convert to uppercase
xxml
Instantiate String^ As <input> = String::Constructor("  Hello World  ");
Instantiate String^ As <trimmed> = Text::StringUtils::trim(input);  // "Hello World"
Instantiate String^ As <lower> = Text::StringUtils::toLowerCase(trimmed);  // "hello world"
Instantiate String^ As <upper> = Text::StringUtils::toUpperCase(trimmed);  // "HELLO WORLD"

Search and Test

MethodParametersReturnsDescription
startsWithstr: String^, prefix: String^Bool^Check prefix
endsWithstr: String^, suffix: String^Bool^Check suffix
containsstr: String^, search: String^Bool^Check substring
indexOfstr: String^, search: String^Integer^First occurrence index (-1 if not found)
lastIndexOfstr: String^, search: String^Integer^Last occurrence index
isEmptystr: String^Bool^Check if empty
isBlankstr: String^Bool^Check if empty or whitespace only
xxml
Instantiate String^ As <path> = String::Constructor("/home/user/file.txt");

If (Text::StringUtils::startsWith(path, String::Constructor("/")).toBool())
{
    Run System::Console::printLine(String::Constructor("Absolute path"));
}

If (Text::StringUtils::endsWith(path, String::Constructor(".txt")).toBool())
{
    Run System::Console::printLine(String::Constructor("Text file"));
}

Instantiate Integer^ As <slashPos> = Text::StringUtils::lastIndexOf(
    path,
    String::Constructor("/")
);

Substrings and Extraction

MethodParametersReturnsDescription
substringstr: String^, start: Integer^, end: Integer^String^Extract substring

Replace and Transform

MethodParametersReturnsDescription
replacestr: String^, old: String^, new: String^String^Replace first occurrence
replaceAllstr: String^, old: String^, new: String^String^Replace all occurrences
repeatstr: String^, count: Integer^String^Repeat string N times
reversestr: String^String^Reverse string
xxml
Instantiate String^ As <text> = String::Constructor("hello world");
Instantiate String^ As <replaced> = Text::StringUtils::replaceAll(
    text,
    String::Constructor("o"),
    String::Constructor("0")
);  // "hell0 w0rld"

Instantiate String^ As <separator> = Text::StringUtils::repeat(
    String::Constructor("-"),
    Integer::Constructor(20)
);  // "--------------------"

Instantiate String^ As <reversed> = Text::StringUtils::reverse(text);  // "dlrow olleh"

Split and Join

MethodParametersReturnsDescription
splitstr: String^, delimiter: String^List<String>^Split by delimiter
joinparts: List<String>^, delimiter: String^String^Join with delimiter
xxml
// Split CSV
Instantiate String^ As <csv> = String::Constructor("apple,banana,cherry");
Instantiate Collections::List<String>^ As <fruits> = Text::StringUtils::split(
    csv,
    String::Constructor(",")
);

// Join with different delimiter
Instantiate String^ As <joined> = Text::StringUtils::join(
    fruits,
    String::Constructor(" | ")
);  // "apple | banana | cherry"

Padding

MethodParametersReturnsDescription
padLeftstr: String^, length: Integer^, pad: String^String^Pad on left
padRightstr: String^, length: Integer^, pad: String^String^Pad on right
xxml
Instantiate String^ As <num> = String::Constructor("42");
Instantiate String^ As <padded> = Text::StringUtils::padLeft(
    num,
    Integer::Constructor(5),
    String::Constructor("0")
);  // "00042"

Instantiate String^ As <label> = Text::StringUtils::padRight(
    String::Constructor("Name"),
    Integer::Constructor(10),
    String::Constructor(" ")
);  // "Name      "

Formatting

MethodParametersReturnsDescription
formattemplate: String^, args: List<String>^String^Format with placeholders
xxml
Instantiate Collections::List<String>^ As <args> = Collections::List@String::Constructor();
Run args.add(String::Constructor("Alice"));
Run args.add(String::Constructor("30"));

Instantiate String^ As <message> = Text::StringUtils::format(
    String::Constructor("Name: {0}, Age: {1}"),
    args
);  // "Name: Alice, Age: 30"

Pattern

Regular expression pattern matching and text manipulation.

Creating Patterns

MethodParametersReturnsDescription
compileregex: String^Pattern^Compile regex pattern (static)

Matching

MethodParametersReturnsDescription
matchtext: String^Bool^Test if entire string matches
findtext: String^String^Find first match
findAlltext: String^List<String>^Find all matches
testtext: String^Bool^Test if pattern appears anywhere
xxml
// Create a pattern for digits
Instantiate Text::Pattern^ As <digits> = Text::Pattern::compile(
    String::Constructor("\\d+")
);

Instantiate String^ As <text> = String::Constructor("Order 123 has 45 items");

// Check if pattern matches anywhere
If (digits.test(text).toBool())
{
    Run System::Console::printLine(String::Constructor("Found digits!"));
}

// Find first match
Instantiate String^ As <first> = digits.find(text);  // "123"

// Find all matches
Instantiate Collections::List<String>^ As <numbers> = digits.findAll(text);
// ["123", "45"]

Replacement

MethodParametersReturnsDescription
replacetext: String^, replacement: String^String^Replace first match
replaceAlltext: String^, replacement: String^String^Replace all matches
splittext: String^List<String>^Split by pattern
xxml
Instantiate Text::Pattern^ As <whitespace> = Text::Pattern::compile(
    String::Constructor("\\s+")
);

Instantiate String^ As <text> = String::Constructor("hello   world   test");

// Replace multiple spaces with single space
Instantiate String^ As <normalized> = whitespace.replaceAll(
    text,
    String::Constructor(" ")
);  // "hello world test"

// Split by whitespace
Instantiate Collections::List<String>^ As <words> = whitespace.split(text);
// ["hello", "world", "test"]

Note

Regex patterns use standard regex syntax. Remember to escape backslashes in XXML strings: use \\\\d for the digit pattern \d.

Complete Example

text_processing.xxml
1#import Language::Core;
2#import Language::Text;
3#import Language::Collections;
4#import Language::System;
5
6[ Entrypoint
7 {
8 // Parse and validate email
9 Instantiate String^ As <email> = String::Constructor(" User@Example.COM ");
10
11 // Clean up input
12 Set email = Text::StringUtils::trim(email);
13 Set email = Text::StringUtils::toLowerCase(email);
14
15 Run System::Console::printLine(
16 String::Constructor("Normalized email: ").append(email)
17 );
18
19 // Validate email format with regex
20 Instantiate Text::Pattern^ As <emailPattern> = Text::Pattern::compile(
21 String::Constructor("^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$")
22 );
23
24 If (emailPattern.match(email).toBool())
25 {
26 Run System::Console::printLine(String::Constructor("Valid email!"));
27
28 // Extract parts
29 Instantiate Collections::List<String>^ As <parts> = Text::StringUtils::split(
30 email,
31 String::Constructor("@")
32 );
33 Run System::Console::printLine(
34 String::Constructor(" Username: ").append(parts.get(Integer::Constructor(0)))
35 );
36 Run System::Console::printLine(
37 String::Constructor(" Domain: ").append(parts.get(Integer::Constructor(1)))
38 );
39 }
40
41 // Parse log line
42 Run System::Console::printLine(String::Constructor(""));
43 Instantiate String^ As <logLine> = String::Constructor(
44 "[2024-01-15 10:30:45] ERROR: Connection failed (code: 500)"
45 );
46
47 // Extract numbers from log
48 Instantiate Text::Pattern^ As <numPattern> = Text::Pattern::compile(
49 String::Constructor("\\d+")
50 );
51 Instantiate Collections::List<String>^ As <numbers> = numPattern.findAll(logLine);
52
53 Run System::Console::printLine(String::Constructor("Numbers in log:"));
54 For (Instantiate Integer^ As <i> = Integer::Constructor(0);
55 i.lessThan(numbers.size()).toBool();
56 Set i = i.add(Integer::Constructor(1)))
57 {
58 Run System::Console::printLine(
59 String::Constructor(" ").append(numbers.get(i))
60 );
61 }
62
63 Exit(0);
64 }
65]

See Also