Skip to content

10 · Strings

String Module

String.upper, String.sub, String.reverse — the functional style equivalent to the methods.

On this page

In addition to method syntax (s.upper()), the String module exposes the same operations as module functions (String.upper(s)). Both forms are equivalent; the choice is a matter of style — method chaining tends to be more idiomatic, while the module form makes it easier to pass functions as values.

The module also provides String.sub (1-based inclusive index substring) and String.reverse, which have no native method form:

String.upper, String.sub, String.reverse and palindrome checking.

09-string-module.zolo
Playground
// Feature: `String` module — functions as a module

// Syntax: `String.method(s, ...)` (alternative to `s.method(...)`)

// When to use: functional style, passing as a first-class function.


use std::String

// In Zolo, string methods have an equivalent module-function form.

// Useful when you want to compose functions.


// -- upper / lower / len ---------------------------------------

print(String.upper("hello"))  // HELLO

print(String.lower("WORLD"))  // world

print(String.len("zolo"))  // 4


// -- sub: substring by range (1-based, inclusive) -------------

print(String.sub("hello", 1, 3))  // hel

print(String.sub("hello", 2, 4))  // ell

print("hello".sub(1, 3))  // hel (method form)


// -- reverse: reverses ----------------------------------------

print(String.reverse("hello"))  // olleh

print(String.reverse("zolo"))  // oloz


// Useful for checking palindromes.

fn is_palindrome(s: str) -> bool {
  return s == String.reverse(s)
}

print(is_palindrome("racecar"))  // true

print(is_palindrome("zolo"))  // false


// -- Functional style vs method --------------------------------

// These two do the same thing:

let s = "Hello World"
print(s.upper())  // HELLO WORLD

print(String.upper(s))  // HELLO WORLD


// Chaining method calls is idiomatic:

let clean = "  HELLO  ".trim().lower()
print(clean)
// expected: hello

Challenge

Use String.sub to extract the domain from "[email protected]" — that is, the part after the @. Hint: locate the index of @ with .chars() and calculate the bounds for sub.

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br