St

Strings in Gleam

49 exercises

About Strings

Strings in Gleam are immutable text surrounded by double quotes. They support unicode characters, and can be multi-line.

let greeting = "Hello, Joe! 📞"

let multi_line_string = "one
two
three"

Strings can be joined together using the <> operator:

let name = "Mike"
"Hello, " <> name <> "!"
// -> "Hello, Mike!"

The gleam/string module in the standard library provides functions for working with strings.

Strings can be matched upon in case expressions:

pub fn on_an_excellent_adventure(name: String) -> Bool {
  case name {
    "Bill" -> True
    "Ted" -> True
    _ -> False
  }
}

If you want to match on the beginning of a string and assign the rest to a variable you can use the <> operator pattern:

pub fn profession(name: String) -> String {
  case name {
    "Dr " <> rest -> rest <> " is a doctor"
    _ -> "I'm not sure what " <> name <> " does"
  }
}
Edit via GitHub The link opens in a new window or tab

Learn Strings

Practicing is locked

Unlock 10 more exercises to practice Strings