Unlike assignment of char *
variables, strings are copied by value, not by reference. String assignment is performed using the =
operator and 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 variable of type string
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 6 bytes of the string "hello"
into it (5 printable characters plus the null byte). String assignment is analogous to the C library function
strcpy
(
3C
)
, except that if the source string exceeds the limit of the storage of the destination string, the resulting string is automatically truncated 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
.