String Operations
Demonstrates Soma's rich set of string built-in functions.
Example 005 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.
How it is built
The cell Strings has no face and no memory. Handlers: run (line 22).
The source
005_strings.cell
// ============================================================================
// 005 — String Operations
// ============================================================================
// Demonstrates Soma's rich set of string built-in functions.
//
// Run: soma run 005_strings.cell run
// Expect:
// Length of 'Hello, Soma!': 12
// Uppercase: HELLO, SOMA!
// Lowercase: hello, soma!
// Trimmed: [Hello]
// Contains 'Soma': true
// Starts with 'Hello': true
// Index of 'Soma': 7
// Substring(0,5): Hello
// Replace: Hello, World!
// Concat: Hello World
// Split: ["a", "b", "c"]
cell Strings {
on run() {
let s = "Hello, Soma!"
// len() returns the string length
let slen = len(s)
print("Length of 'Hello, Soma!': {slen}")
// Case conversion
let up = uppercase(s)
print("Uppercase: {up}")
let low = lowercase(s)
print("Lowercase: {low}")
// trim() removes leading/trailing whitespace
let padded = " Hello "
let trimmed = trim(padded)
print("Trimmed: [{trimmed}]")
// Search functions
let has_soma = contains(s, "Soma")
print("Contains 'Soma': {has_soma}")
let starts = starts_with(s, "Hello")
print("Starts with 'Hello': {starts}")
let idx = index_of(s, "Soma")
print("Index of 'Soma': {idx}")
// Extraction
let sub = substring(s, 0, 5)
print("Substring(0,5): {sub}")
// Transformation
let replaced = replace(s, "Soma", "World")
print("Replace: {replaced}")
// Concatenation
let combined = concat("Hello", " World")
print("Concat: {combined}")
// Splitting
let parts = split("a,b,c", ",")
print("Split: {parts}")
return "done"
}
}What the toolchain says
$ soma check 005_strings.cell
✓ All checks passed.$ soma run 005_strings.cell run
Length of 'Hello, Soma!': 12
Uppercase: HELLO, SOMA!
Lowercase: hello, soma!
Trimmed: [Hello]
Contains 'Soma': true
Starts with 'Hello': true
Index of 'Soma': 7
Substring(0,5): Hello
Replace: Hello, World!
Concat: Hello World
Split: ["a", "b", "c"]
done