From ef024b9bbe231d1ee6db30b90d76d33e2be3947b Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sat, 3 Oct 2020 12:27:27 +0530 Subject: [PATCH 1/2] strings: update information with latest V --- en/examples/section_1/strings.md | 41 ++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/en/examples/section_1/strings.md b/en/examples/section_1/strings.md index c5639b6..ab83c14 100644 --- a/en/examples/section_1/strings.md +++ b/en/examples/section_1/strings.md @@ -5,7 +5,7 @@ example_title: Strings # 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. +In V one can define strings using the `:=` operator. Strings (like other variables) are immutable by default i.e you cannot change its value. String are also null terminated with `\0` for easy C interpolation. 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. ```go name := 'Bob' @@ -13,6 +13,15 @@ println(name) // Bob println(name.len) // 3 ``` +String can also be multi-lined: + +```go +name := ' +this is a multi line +string +' +println(name) +``` Getting the length of a string works with `.len`. ## Interpolation @@ -87,7 +96,14 @@ 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. +To define character literals use: ` `` `. +```go +b := `A` // default type is rune +a := `byte(`A`) // type is now byte +``` + + +Raw strings can be defined as prepending `r`. They are not escaped. ```go hello := 'Hello\nWorld' @@ -96,3 +112,24 @@ println(hello) // Hello raw_hello := r'Hello\nWorld' println(raw_hello) // Hello\nWorld ``` + +You can also define C-strings with `c` as prefix for C interpolation. Remember that they have to be null-terminated. + +```go +c_str := c'hello\0' +println(c_str) +``` + +**NOTE**: raw and c strings are not usuable with character literals. + +If you want to change the content of the string then you can do so in `unsafe` expression: + +```go +mut name := 'Bob' // mut is required to change +println(name) // Bob +unsafe { + name[0] = `D` + name[2] = `g` +} +println(name) // Dog +``` From d1c50d5e28d017a2fb27c2ebe778c13ba718aeb6 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 4 Oct 2020 20:32:09 +0530 Subject: [PATCH 2/2] string: update info --- en/examples/section_1/strings.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/en/examples/section_1/strings.md b/en/examples/section_1/strings.md index ab83c14..a937498 100644 --- a/en/examples/section_1/strings.md +++ b/en/examples/section_1/strings.md @@ -5,7 +5,7 @@ example_title: Strings # Strings -In V one can define strings using the `:=` operator. Strings (like other variables) are immutable by default i.e you cannot change its value. String are also null terminated with `\0` for easy C interpolation. 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. +In V one can define strings using the `:=` operator. Strings (like other variables) are immutable by default i.e you cannot change its value. String are also null (`\0`) terminated for easy C interpolation. 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. ```go name := 'Bob' @@ -113,16 +113,16 @@ raw_hello := r'Hello\nWorld' println(raw_hello) // Hello\nWorld ``` -You can also define C-strings with `c` as prefix for C interpolation. Remember that they have to be null-terminated. +You can also define C-strings with `c` as prefix for C interpolation. They are null terminated by default. ```go -c_str := c'hello\0' +c_str := c'hello' println(c_str) ``` **NOTE**: raw and c strings are not usuable with character literals. -If you want to change the content of the string then you can do so in `unsafe` expression: +If you want to change the contents of the string then you can do so in `unsafe` expression: ```go mut name := 'Bob' // mut is required to change