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
  • 式展開(interpolation)
  • 文字列の結合
  • 注意

Was this helpful?

  1. コード例で学ぶV言語
  2. examples
  3. section_1

文字列

Vでは文字列の定義も:=演算子で行えます。他の変数と同様、文字列もデフォルトでイミュータブルです。文字列を(リテラルとして)表現するときには""や''のどちらでも使えます。vfmtで書式を整えると、文字列リテラルを囲む""は、文字列の中に'を含んでいなければすべて''に変換されます。

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

文字列の長さは.lenで取得できます。

式展開(interpolation)

文字列の中で$に続けて変数名を書くと、変数の値を文字列に展開できます。

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!'      // helloに保存されている文字列に'from V!'を追加する
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

注意

some_string[開始位置..終了位置]という構文の終了位置は、終了位置そのものは含みません(not inclusive)。

Vのどの演算子についても、両辺に同じ型の値が必ず存在しなければなりません。以下のコードは、ageがint型なのでコンパイルされません。

age := 25
println('age = ' + age) // cannot convert `int` to `string`

つまり、.str()で文字列に変換するか、式展開を使う必要があります。Vでは式展開が推奨されています。

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

文字リテラルを定義するには`` を用います。raw stringの冒頭にrを付けるとエスケープされなくなります。

hello := 'Hello\nWorld'
println(hello)                  // Hello
                                // World
raw_hello := r'Hello\nWorld'
println(raw_hello)              // Hello\nWorld
Previous書式付き出力Nextコメント

Last updated 5 years ago

Was this helpful?