Strings
Overview
Like many languages, V has built-in support for strings. Like Go, V uses UTF-8 to encode strings. All strings in V are an immutable byte array.
Strings in V are specified using single or double quotes. Both options are equivalent; however, it is recommended to use single quotes for strings that do not contain single quotes.
That is, as long as the string does not contain other single quotes, it is better to use single quotes:
But if the string contains single quotes, then it is better to use double quotes to avoid having to escape the single quotes:
vfmt will automatically convert double quotes to single quotes when possible.
Escaping in strings is supported as in C:
Since strings are stored in UTF-8, any characters can be used, including emoji:
Arbitrary bytes can be directly specified using \x##
notation where #
is a hex digit:
Or using octal escape \###
notation where #
is an octal digit:
Unicode can be specified directly as \u####
where # is a hex digit and will be converted
internally
to its UTF-8 representation:
Since all characters in V are stored in UTF-8, the length of the string may differ from the number of visible characters in it:
String values are immutable. You cannot mutate elements:
Note that indexing a string will produce a byte
, not a rune
nor another string
.
Indexes correspond to bytes in the string, not Unicode code points.
If you want to convert the byte
to a string
, use the .ascii_str()
method on the byte
:
String conversion
int
or other numeric types
to To get an int from a string, use the .int()
or .<type-name>()
method:
All int literals are supported:
f32
or f64
to To get a float from a string, use the .f32()
or .f64()
method:
bool
to To get a bool from a string, use the .bool()
method:
[]u8
to To get an array of bytes from a string, use the .bytes()
method:
[]u8
from To turn an array of bytes into a string, use the .bytestr()
method:
[]rune
to To get an array of runes from a string, use the .runes()
method:
[]rune
from To turn an array of runes into a string, use the .string()
method:
For more advanced string
processing and conversions, refer to the
vlib/strconv
module.
Raw strings
V also supports "raw" strings, which do not handle escaping and do not support string interpolation.
For raw strings, prepend string literal with r
:
C strings
V also supports C strings, which are null-terminated arrays of bytes.
For raw strings, prepend string literal with c
:
String interpolation
Basic interpolation syntax is pretty simple – use ${
before a variable name and }
after.
The variable will be converted to a string and embedded into the literal:
It also works with fields:
And with complex expressions:
Format specifiers
To control the output format, you can use format specifiers like those used in C in printf()
.
Format specifiers are optional and specify the output format.
The compiler takes care of the storage size, so there is no hd
or llu
.
To use a format specifier, follow this pattern:
${varname:[flags][width][.precision][type]}
Flags
Zero or more of the following:
-
– left-align output within the field. The default is to right-align.0
– use0
as the padding character instead of the defaultspace
character.
V does not currently support the use of
'
or#
as format flags, and V supports but doesn't need+
to right-align since that's the default.
Width
Integer value describing the minimum width of total field to output.
Precision
Integer value preceded by a .
will guarantee that many digits after the decimal
point, if the input variable is a float.
Ignored if variable is an integer.
Type
f
andF
specify the input is a float and should be rendered as such.e
andE
specify the input is a float and should be rendered as an exponent (partially broken).g
andG
specify the input is a float--the renderer will use floating point notation for small values and exponent notation for large values.d
specifies the input is an integer and should be rendered in base-10 digits.x
andX
require an integer and will render it as hexadecimal digits.o
requires an integer and will render it as octal digits.b
requires an integer and will render it as binary digits.s
requires a string (almost never used).
When a numeric type can render alphabetic characters, such as hex strings or special values like
infinity
, the lowercase version of the type forces lowercase alphabetic and the uppercase version forces uppercase alphabetic.
In most cases, it is best to leave the format type empty.
Floats will be rendered by default as g
, integers will be rendered
by default as d
, and s
is almost always redundant.
There are only three cases where specifying a type is recommended:
- format strings are parsed at compile time, so specifying a type can help detect errors then
- format strings default to using lowercase letters for hex digits and the
e
in exponents. Use an uppercase type to force the use of uppercase hex digits and an uppercaseE
in exponents. - format strings are the most convenient way to get hex, binary, or octal strings from an integer.
See Format Placeholder Specification for more information.
String operators
V defines the +
and +=
operators for strings:
+
– used for string concatenation
+=
– used to append to a string
All operators in V must have values of the same type on both sides. You cannot concatenate an integer to a string:
To concatenate a string with a number, you must first convert the number to a string.
or use string interpolation (preferred):
See all methods of
string
and related modules
strings
,
strconv
.