Skip to content

Strings ​

Instance methods on string values.

Methods ​

MethodReturnsDescription
len()intLength in bytes
upper()stringUppercase
lower()stringLowercase
trim()stringStrip whitespace
trim_left()stringStrip leading whitespace
trim_right()stringStrip trailing whitespace
contains(s)boolSubstring check
starts_with(s)boolPrefix check
ends_with(s)boolSuffix check
index_of(s)intFirst occurrence (-1 if not found)
last_index_of(s)intLast occurrence
substring(start, end)stringExtract range
replace(old, new)stringReplace all occurrences
repeat(n)stringRepeat n times
* n (operator)stringRepeat n times: "ha" * 3 → "hahaha"
reverse()stringReverse characters
capitalize()stringFirst char uppercase
title()stringTitle case
pad_left(n, ch)stringLeft-pad to width
pad_right(n, ch)stringRight-pad to width
split(delim)[string]Split into array
count(s)intCount occurrences
to_int()intParse integer
is_alpha()intAll alphabetic (1/0)
is_digit()intAll numeric (1/0)

Examples ​

wyn
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 ​

wyn
clean = "  HELLO  ".trim().lower().reverse()
print(clean)  // olleh

Interpolation ​

wyn
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:

wyn
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:

wyn
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:

wyn
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

MIT License - v1.22.0