MoonBit Language Tour MoonBit

String And Char

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

Char is a single Unicode character, represented by a single quote, for example 'a'.

Escape sequences and Unicode

To represent special characters, MoonBit supports C-style escape sequences in string and char literals, 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.

String interpolation and concatenation

MoonBit also supports string interpolation, written as \{variable}, which allows you to embed expressions into strings. You can also concatenate strings using the + operator.

String is a complex type in real-world programs. This lesson introduces the basics, but there are many advanced features that will be covered later.

///|
fn main {
  let a : String = "Hello, world!"
  let b : Char = 'H'
  println(a)
  println(b)

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

  // Concatenate two strings.
  let moon = "Moon"
  let bit = "Bit"
  let moonbit = moon + bit
  println(" Hello," + moonbit)

  // Use string interpolation.
  println("Use \{moon}\{bit}. Happy coding")
}