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
  • Concatenation
  • Notes

Was this helpful?

  1. en
  2. examples
  3. section_1

Strings

In V one can define strings using the := operator. Strings (like other variables) are immutable by default. One is free to use "" or '' to denote a string. When using vfmt all double-quoted strings will be converted to single-quoted ones unless it contains a single quote character.

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

Getting the length of a string works with .len.

Interpolation

It is possible to do string interpolation with $ in front of the variable:

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

One can have more complex expressions with interpolation syntax by using ${}:

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

Concatenation

Strings can be concatenated with the + operator.

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

Appending to a string works with concatenation as well as with += operator. Since strings are immutable by default it is only possible to do this if they are declared with mut.

mut hello := 'Hello '
hello += 'from V!'      // appends 'from V!' to the string stored in hello.
println(hello)          // Hello from V!

In V, string data is encoded using UTF-8 and the string itself is a read-only array of bytes. This makes slicing possible, which means we can access single-character literals or slices of a string variable.

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

When using some_string[start..end] syntax the end is not inclusive.

All operators in V must have values of the same type on both sides. The code below will not compile because age is an int:

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

We therefore need to convert it to string by using .str() or use string interpolation (preferred):

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

To define character literals use: `` . Raw strings can be defined as prepending r. They are not escaped.

hello := 'Hello\nWorld'
println(hello)                  // Hello
                                // World
raw_hello := r'Hello\nWorld'
println(raw_hello)              // Hello\nWorld
PreviousFormatted PrintNextComments

Last updated 4 years ago

Was this helpful?