LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • 插入
  • 连接操作符
  • Notes

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_1

字符

在V中,可以使用:=运算符定义字符串。默认情况下,字符串(与其他变量一样)是不可变的。 一个可以使用""或 ''来表示字符串。使用vfmt时,除非包含单引号字符,否则所有双引号字符串都将转换为单引号字符串。

name := 'Bob'
println(name)       // Bob
println(name.len)   // 3

获取字符串的长度使用.len。

插入

可以在变量前面使用$进行字符串插值:

name:= 'Bob'
println('Hello $name!')     // Hello Bob!

使用${}可以有更复杂的插值语法表达式:

struct User {
    name string
    age int
}
bob := User {
    name: 'Bob'
    age: 17
}
println('Say Hello to a new User: ${bob.name}, ${bob.age}')             // Say Hello to new User: Bob, 17
println('${bob.name}s age is higher or equal to 18: ${bob.age >= 18}')  // 0 <=> number representation for false

连接操作符

使用+运算符符字符串会被连接。

text := 'Hello'
concatenated_text := text + ' World!'
println(text)                   // Hello
println(text + ' World!')       // Hello World!
println(concatenated_text)      // Hello World!

添加到字符串与连接可以使用+=运算符。因为字符串在默认情况下是不可变的,所以只有用mut声明它们时才能这样做。

mut hello := 'Hello '
hello += 'from V!'      // 附加“从V!”到hello中存储的字符串。
println(hello)          // Hello from V!

在V中,字符串数据使用UTF-8编码,字符串本身是一个只读字节数组。这使得切片成为可能,这意味着我们可以访问单个字符的文本或字符串变量的切片。

robert := 'Robert'
bert := robert[2..robert.len]                                 // bert
rob := robert[0..3]                                           // Rob
println('The persons of interest are: $robert, $bert, $rob')  // The persons of interest are: Robert, bert, Rob

Notes

V中的所有运算符两边必须具有相同类型的值。下面的代码无法编译,因为age是int类型的:

age := 25
println('age = ' + age)

因此,我们需要使用.str()或使用字符串插值(最好用的方法)将其转换为字符串:

age := 25
println('age = ' + age.str())   // age = 25
println('age = $age')           // age = 25

要定义字符,可以使用:`` 。原始字符串可以定义为前缀r。

hello := 'Hello\nWorld'
println(hello)                  // Hello
                                // World
raw_hello := r'Hello\nWorld'
println(raw_hello)              // Hello\nWorld
Previousprint系列函数Next注释

Last updated 5 years ago

Was this helpful?