Unlike the assignment of char *
variables,
strings are copied by value and not by reference. The string
assignment operator =
copies the actual bytes
of the string from the source operand up to and including the
null byte to the variable on the left-hand side, which must be
of type string
. You can create a new string
variable by assigning it an expression of type
string
.
For example, the D statement:
s = "hello";
would create a new variable s
of type
string
and copy the six bytes of the string
"hello"
into it (five printable characters,
plus the null byte). String assignment is analogous to the C
library function strcpy()
, with the exception
that if the source string exceeds the limit of the storage of
the destination string, the resulting string is automatically
truncated by a null byte at this limit.
You can also assign to a string variable an expression of a type
that is compatible with strings. In this case, the D compiler
automatically promotes the source expression to the string type
and performs a string assignment. The D compiler permits any
expression of type char *
or of type
char[n]
, that is, a scalar array of
char
of any size, to be promoted to a string.