String Methods
Strings in Oxide use Rust's standard String and &str methods directly. Oxide's case conversion automatically translates camelCase method calls to Rust's snake_case equivalents.
Methods
| Method | Rust Equivalent | Description |
|---|---|---|
isEmpty() | is_empty() | Returns true if the string is empty |
toUppercase() | to_uppercase() | Returns uppercase version |
toLowercase() | to_lowercase() | Returns lowercase version |
trim() | trim() | Removes leading/trailing whitespace |
startsWith(prefix) | starts_with(prefix) | Checks prefix |
endsWith(suffix) | ends_with(suffix) | Checks suffix |
contains(substring) | contains(substring) | Checks if contains substring |
repeat(count) | repeat(count) | Repeats string n times |
split(delimiter) | split(delimiter) | Splits by delimiter |
replace(from, to) | replace(from, to) | Replaces all occurrences |
chars() | chars() | Character iterator |
len() | len() | Byte length |
Examples
Basic Operations
let greeting = " Hello, World! "
// Trimming
let trimmed = greeting.trim() // "Hello, World!"
// Case conversion
let upper = greeting.toUppercase() // " HELLO, WORLD! "
let lower = greeting.toLowercase() // " hello, world! "
// Empty check
greeting.isEmpty() // false
"".isEmpty() // true
Prefix and Suffix
let filename = "document.pdf"
filename.startsWith("doc") // true
filename.endsWith(".pdf") // true
filename.endsWith(".txt") // false
Contains
let text = "The quick brown fox"
text.contains("quick") // true
text.contains("slow") // false
text.contains("") // true (empty string)
Repeat
let star = "*"
star.repeat(5) // "*****"
let ha = "ha"
ha.repeat(3) // "hahaha"
"x".repeat(0) // ""
Split
let csv = "apple,banana,cherry"
for part in csv.split(",") {
println!(part)
}
let lines = "a\nb\nc".split("\n")
for line in lines {
println!(line)
}
// Split on multiple characters
let path = "a/b/c/d"
for segment in path.split("/") {
println!(segment)
}
Replace
let text = "hello world"
// Simple replacement
text.replace("world", "Oxide") // "hello Oxide"
// Replace all occurrences
let zigzag = "ababab"
zigzag.replace("ab", "X") // "XXX"
// Chain replacements
text.replace("l", "L")
.replace("o", "0") // "heLL0 w0rLd"
Characters Iterator
let word = "hello"
// Iterate over characters
for char in word.chars() {
println!(char)
}
// Count characters
word.chars().count() // 5
// Unicode-aware
let emoji = "👋🌍"
emoji.chars().count() // 2
Comparison with Rust
| Oxide | Rust |
|---|---|
s.isEmpty() | s.is_empty() |
s.toUppercase() | s.to_uppercase() |
s.toLowercase() | s.to_lowercase() |
s.trim() | s.trim() |
s.startsWith(p) | s.starts_with(p) |
s.endsWith(s) | s.ends_with(s) |
s.contains(sub) | s.contains(sub) |
s.repeat(n) | s.repeat(n) |
s.split(d) | s.split(d) |
s.replace(a, b) | s.replace(a, b) |
s.chars() | s.chars() |
String Interpolation
While not part of string methods, string interpolation is a core Oxide feature:
let name = "Alice"
let age = 30
// Display interpolation
let greeting = "Hello, $name!"
let info = "Age: ${age * 12} months"
// Debug interpolation
let numbers = [1, 2, 3]
println!("Numbers: #numbers") // Uses {:?} format
Additional Rust Methods
Since Oxide uses Rust's string methods directly, you can use any method from Rust's standard library. Here are some common ones:
let s = "Hello, World!"
// Length and capacity
s.len() // byte length
s.chars().count() // character count
// Searching
s.find("World") // Some(7) - byte index
s.rfind("o") // Some(8) - last occurrence
// Parsing
"42".parse<Int>() // Ok(42)
// Trimming variants
" hello ".trimStart() // "hello " (Rust: trim_start)
" hello ".trimEnd() // " hello" (Rust: trim_end)
// Lines
"a\nb\nc".lines() // iterator over lines
See Also
- Literals - String literal syntax
- Standard Library Overview