Working with Strings
Create, manipulate, and format text in your calculations
Equana provides a simple but powerful string system. Unlike traditional character arrays, Equana uses true strings — making text manipulation more intuitive and predictable.
This tutorial covers string creation, manipulation, and formatting with interactive examples you can run and modify.
String Literals
Create strings using double quotes:
# Double quotes for strings
greeting = "Hello, World!"
println(greeting)
# Another string
message = "Welcome to Equana"
println(message)Tip: Double-quoted strings are the standard way to define text in Equana.
String Interpolation
Embed expressions directly in double-quoted strings using ${expression}:
name = "Alice"
age = 28
score = 95.5
# Embed variables directly in strings
intro = "My name is ${name} and I'm ${age} years old."
println(intro)
# Embed calculations
result = "Score: ${score}% (passed: ${score >= 90})"
println(result)String interpolation automatically converts numbers, booleans, and other values to strings. This is also the primary way to combine strings and other values together.
Escape Sequences
Use backslash escapes to include special characters in your strings:
| Escape | Character |
|---|---|
\n | Newline |
\t | Tab |
\\ | Backslash |
\" | Double quote |
\$ | Dollar sign (prevents interpolation) |
# Newlines and tabs
formatted = "Line 1\nLine 2\nLine 3"
println(formatted)
# Include quotes in strings
quote = "She said \"Hello!\""
println(quote)
# Prevent interpolation with \$
literal = "Use \${name} for interpolation"
println(literal)Combining Strings
Use string interpolation to combine strings and values together:
first = "Hello"
second = "World"
# Use interpolation to join strings
combined = "${first}, ${second}!"
println(combined)
# Interpolation handles numbers automatically
answer = "The answer is ${42}"
println(answer)
# Build complex strings with expressions
x = 3
y = 4
println("${x} + ${y} = ${x + y}")String Comparison
Compare strings using comparison operators:
a = "apple"
b = "banana"
# Equality comparison
println(a == "apple") # true
println(a == b) # false
# Lexicographic comparison
println(a < b) # true (apple comes before banana)String Functions Reference
Equana provides 10 built-in string functions:
| Function | Description | Example | Result |
|---|---|---|---|
lowercase(s) | Convert to lowercase | lowercase("HELLO") | "hello" |
uppercase(s) | Convert to uppercase | uppercase("hello") | "HELLO" |
trim(s) | Remove whitespace | trim(" hi ") | "hi" |
split(s, delim) | Split string | split("a,b,c", ",") | ["a", "b", "c"] |
join(arr, delim) | Join array | join(["a", "b", "c"], "-") | "a-b-c" |
replace(s, old, new) | Replace substring | replace("hello", "l", "x") | "hexxo" |
startswith(s, prefix) | Check prefix | startswith("hello", "he") | true |
endswith(s, suffix) | Check suffix | endswith("hello", "lo") | true |
contains(s, sub) | Check if contains | contains("hello", "ell") | true |
includes(x, val) | Contains (arrays too) | includes([1, 2, 3], 2) | true |
Extended Examples
Building Dynamic Messages
Use string interpolation for formatted output:
items = 5
price = 9.99
total = items * price
msg = "You bought ${items} items for $${total}"
println(msg)Text Processing
Chain string functions for text processing:
text = " Hello World "
# Clean up whitespace
cleaned = trim(text)
println(cleaned)
# Split into words
words = split(cleaned, " ")
println(words)
# Convert to uppercase
println(uppercase(join(words, "_")))Finding and Replacing
Search and modify strings with find/replace functions:
sentence = "The quick brown fox jumps over the lazy dog"
# Replace words
modified = replace(sentence, "fox", "cat")
println(modified)
# Check content
println(contains(sentence, "fox"))
println(startswith(sentence, "The"))Summary
This tutorial covered the essentials of working with strings in Equana:
Key Concepts
- String Literals — Use double quotes for all strings
- String Interpolation — Embed expressions with
${...}directly in double-quoted strings - Escape Sequences — Include special characters like \n and \t
- Combining Strings — Use interpolation to join strings and values together
Most Used Functions
uppercase()/lowercase()— Case conversiontrim()— Remove whitespacesplit()/join()— Split and join stringsreplace()— Find and replacecontains()/startswith()/endswith()— String testing
Further Reading
- Browse the complete function reference for detailed documentation
- Experiment in the notebook with your own string operations