String Assignment
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 side, which must be of type string. You can create a new variable of type string
by assigning it an expression of type string
. The D statement, s = "hello";
, creates 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).
s = "hello";
String assignment is analogous to the C library function strcpy
(), 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. For more information, see the strcpy
(3C) man page.
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.