Documentation
Bop language guide

Strings

Strings🔗

Strings are immutable sequences of characters. All string methods return new strings — the original is never modified.

Creating strings🔗

let s = "hello world"
let empty = ""
let escaped = "Line 1\nLine 2"

Supported escape sequences: \", \\, \n, \t, \r, \{, \}. Any other \x escape is a lexer error.

Indexing🔗

let s = "hello"
print(s[0])      // "h"
print(s[-1])     // "o"

Each index returns a single-character string (there’s no separate character type).

String interpolation🔗

Insert variable values with {name} inside a string:

let name = "Alice"
let count = 5
print("Hello, {name}! You have {count} items.")

Only variable names are allowed inside {}. For expressions, use a temporary variable:

let total = (count * 2).to_str()
print("Double: {total}")

Or use concatenation:

print("Double: " + (count * 2).to_str())

To include a literal { or } in a string, escape it with \{ and \}:

print("Use \{name\} for interpolation")
// prints: Use {name} for interpolation

Concatenation🔗

Use + to join strings:

let full = "Hello" + ", " + "world!"
print(full)    // "Hello, world!"

Numbers must be converted with .to_str() first:

let msg = "Score: " + (42).to_str()

Methods🔗

MethodReturnsDescription
s.len()intNumber of characters
s.contains(sub)boolWhether the string contains sub
s.starts_with(prefix)boolWhether it starts with prefix
s.ends_with(suffix)boolWhether it ends with suffix
s.index_of(sub)intIndex of first occurrence, or -1
s.split(sep)arraySplit into array of strings on sep
s.replace(old, new)stringReplace all occurrences
s.upper()stringUppercase copy
s.lower()stringLowercase copy
s.trim()stringCopy with leading/trailing whitespace removed
s.slice(start, end)stringHalf-open substring by code-point index. Negative bounds count from the end; out-of-range bounds clamp
s.to_int()intParse. "3.7".to_int()3 (float-then-truncate). Raises on junk.
s.to_float()numberParse. Raises on junk.

Plus the universal s.type(), s.to_str(), s.inspect().

Practical examples🔗

Parsing CSV data🔗

let input = "Alice,95,A"
let parts = input.split(",")
print(parts[0])    // "Alice"
print(parts[1])    // "95"

Checking prefixes🔗

let filename = "report.csv"
if filename.ends_with(".csv") {
  print("CSV file detected")
}

Building a formatted string🔗

let items = ["apple", "banana", "cherry"]
let count = items.len().to_str()
let list = items.join(", ")
print("Found {count} items: {list}")
Found something unclear?Edit this page on GitHub
Documentation

Search Bop

Start typing to search the guide and reference.