Strings
Instance methods on string values.
Methods
| Method | Returns | Description |
|---|---|---|
len() | int | Length in bytes |
upper() | string | Uppercase |
lower() | string | Lowercase |
trim() | string | Strip whitespace |
trim_left() | string | Strip leading whitespace |
trim_right() | string | Strip trailing whitespace |
contains(s) | bool | Substring check |
starts_with(s) | bool | Prefix check |
ends_with(s) | bool | Suffix check |
index_of(s) | int | First occurrence (-1 if not found) |
last_index_of(s) | int | Last occurrence |
substring(start, end) | string | Extract range |
replace(old, new) | string | Replace all occurrences |
repeat(n) | string | Repeat n times |
* n (operator) | string | Repeat n times: "ha" * 3 → "hahaha" |
reverse() | string | Reverse characters |
capitalize() | string | First char uppercase |
title() | string | Title case |
pad_left(n, ch) | string | Left-pad to width |
pad_right(n, ch) | string | Right-pad to width |
split(delim) | [string] | Split into array |
count(s) | int | Count occurrences |
to_int() | int | Parse integer |
is_alpha() | int | All alphabetic (1/0) |
is_digit() | int | All numeric (1/0) |
Examples
s = "Hello, World!"
print(s.upper()) // HELLO, WORLD!
print(s.lower()) // hello, world!
print(s.contains("World").to_string()) // true
print(s.replace("World", "Wyn")) // Hello, Wyn!
print(s.split(", ").join(" | ")) // Hello | World!Chaining
clean = " HELLO ".trim().lower().reverse()
print(clean) // ollehInterpolation
name = "Wyn"
print("Hello, ${name}!")Concatenation
+ joins two strings. It also accepts an int on either side, which is converted to its decimal text - this is deliberate, and it is part of Wyn feeling like Python rather than like C:
fn main() {
print(5 + "x") // 5x
print("x" + 5) // x5
name = "Alice"
age = 30
print(name + " is " + age + " years old")
}+ is left-associative, so the numbers are added before the string appears and appended after it: 1 + 2 + "x" is "3x", while "x" + 1 + 2 is "x12".
Only int converts. A float or a bool beside a string is a type error - call .to_string() on those:
fn main() {
ratio = 3.14
print("pi is " + ratio.to_string())
ok = true
print(ok.to_string())
}For anything longer than a two-part label, reach for ${} interpolation instead. It reads better and it never leaves you counting + signs:
fn main() {
name = "Alice"
age = 30
ratio = 3.14
print("${name} is ${age} years old and ${ratio} is pi")
}Interpolation takes every type without a conversion call, so it is the form to prefer in new code. + with an int stays supported - it is not deprecated, and it is often the clearest thing to write for a short label.
One performance note that is unrelated to types: building a long string by repeated s = s + "x" in a loop is quadratic, because strings are immutable. Use StringBuilder for that - see Benchmarks.
See Also
- Arrays -
.split()returns arrays,.join()takes arrays - API Reference - complete method listing
- Regex - pattern matching on strings
- Encoding - Base64 encode/decode strings