MoonBit Language Tour MoonBit

String

A string is a sequence of characters encoded in UTF-16. In MoonBit, strings are immutable, which means you cannot change the elements inside a string.

MoonBit supports C-style escape characters in strings and chars, such as \n (newline), \t (tab), \\ (backslash), \" (double-quote), and \' (single-quote).

Unicode escape characters are also supported. You can use \u{...} (where ... represents the Unicode character's hex code) to represent a Unicode character by its code point.

MoonBit also supports string interpolation written like \{variable}, which allows you to embed expressions into strings.

fn main {
  let str = "Hello, World!"
  // Access a character by index.
  let c : Char = str[4]
  println(c)
  let c2 = 'o'
  println(c == c2)

  // Use escape sequence.
  println("\nHello, \tWorld!")
  println("unicode \u{1F407} is a rabbit")

  // Concatenate two strings.
  println(str + " Hello, MoonBit!")

  // Use string interpolation.
  let moon = "Moon"
  let bit = "Bit"
  println("Use \{moon + bit}. Happy coding")
}