Equana

Back to Tutorials

Working with Strings

Create, manipulate, and format text in your calculations

Run AllReset

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:

Code [1]Run
# 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}:

Code [2]Run
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:

EscapeCharacter
\nNewline
\tTab
\\Backslash
\"Double quote
\$Dollar sign (prevents interpolation)
Code [3]Run
# 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:

Code [4]Run
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:

Code [5]Run
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:

FunctionDescriptionExampleResult
lowercase(s)Convert to lowercaselowercase("HELLO")"hello"
uppercase(s)Convert to uppercaseuppercase("hello")"HELLO"
trim(s)Remove whitespacetrim(" hi ")"hi"
split(s, delim)Split stringsplit("a,b,c", ",")["a", "b", "c"]
join(arr, delim)Join arrayjoin(["a", "b", "c"], "-")"a-b-c"
replace(s, old, new)Replace substringreplace("hello", "l", "x")"hexxo"
startswith(s, prefix)Check prefixstartswith("hello", "he")true
endswith(s, suffix)Check suffixendswith("hello", "lo")true
contains(s, sub)Check if containscontains("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:

Code [6]Run
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:

Code [7]Run
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:

Code [8]Run
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 conversion
  • trim() — Remove whitespace
  • split() / join() — Split and join strings
  • replace() — Find and replace
  • contains() / startswith() / endswith() — String testing

Further Reading

Workbench

Clear
No variables in workbench

Next Steps