Literals/String: Difference between revisions
m
no edit summary
mNo edit summary |
|||
Line 3,119:
<syntaxhighlight lang="vbnet">Dim s = "Tom said, ""The fox ran away."""
Result: Tom said, "The fox ran away."</syntaxhighlight>
=={{header|V (Vlang)}}==
Character literals for Unicode characters, "rune literals", are an alias for u32.
Character literals for UTF-8 characters, "string literals", are an alias for u8.
To denote rune, Unicode characters, ` (backticks) are used :
A rune can be converted to a UTF-8 string by using the .str() method.
rocket := `🚀`
rocket.str() == '🚀' // uses single quotes, not the backtick, after conversion
A string can be converted back to runes by the .runes() method.
hello := 'Hello World'
hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`]
In V, a string is an immutable array of read-only bytes. All Unicode characters are encoded using UTF-8:
mut s := 'hello 🌎'
s[0] = `H` // not allowed as immutable
// convert `string` to `[]u8`
s := 'hello 🌎'
arr := s.bytes()
assert arr.len == 10
// convert `[]u8` to `string`
s2 := arr.bytestr()
assert s2 == s
// indexing gives a byte, u8(66) == `B`
name := 'Bob'
println(name.len == 3) // will print 3
if name[0] == u8(66) {println(name[0].ascii_str())} // will print`B`
String literals are contained in quotes:
str:= "Hello, world!"
=={{header|WEB}}==
|