MoonBit 语言导览 MoonBit

字符串

字符串是UTF-16编码的字符序列。在MoonBit中,字符串是不可变的,这意味着无法直接修改字符串中的字符。

MoonBit支持字符串和字符中的C风格转义字符,例如\n(换行符)、\t(制表符)、\\(反斜杠)、\"(双引号)和\'(单引号)。

Moonbit同时也支持Unicode转义字符,可使用\u{...}表示(其中...代表Unicode字符的十六进制码点)。

MoonBit还支持\{变量}形式的字符串插值语法,允许在字符串中嵌入表达式。

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")
}