Additionally, if one has an array of floats, is '+' overloaded to be the same as a vector addition, or an array concatenation?
In D, these problems are avoided by introducing a new binary operator ~ as the concatenation operator. It works with arrays (of which strings are a subset). ~= is the corresponding append operator. ~ on arrays of floats would concatenate them, + would imply a vector add. Adding a new operator makes it possible for orthogonality and consistency in the treatment of arrays. (In D, strings are simply arrays of characters, not a special type.)
const char abc[5] = "world"; string str = "hello" + abc;That isn't going to work. But it does work when the core language knows about strings:
const char[5] abc = "world"; char[] str = "hello" ~ abc;
const char abc[] = "world"; : sizeof(abc)/sizeof(abc[0])-1 : strlen(abc) string str : str.length()That kind of inconsistency makes it hard to write generic templates. Consider D:
char[5] abc = "world"; : abc.length char[] str : str.length
string str; if (str.empty()) // string is emptyIn D, an empty string is just null:
char[] str; if (!str) // string is empty
string str; str.resize(newsize);D takes advantage of knowing that str is a string, and so resizing it is just changing the length property:
char[] str; str.length = newsize;
string s1 = "hello world"; string s2(s1, 6, 5); // s2 is "world"D has the array slice syntax, not possible with C++:
char[] s1 = "hello world"; char[] s2 = s1[6 .. 11]; // s2 is "world"Slicing, of course, works with any array in D, not just strings.
string s1 = "hello world"; string s2 = "goodbye "; s1.copy(s2, 1, 5); // s2 is "worldye "D allows copying anywhere in the string:
char[] s1 = "hello world"; char[] s2 = "goodbye "; s2[8..13] = s1[6..11]; // s2 is "goodbye world"
void foo(const char *); string s1; foo(s1.c_str());In D, strings can be implicitly converted to char*:
void foo(char *); char[] s1; foo(s1);Note: some will argue that it is a mistake in D to have an implicit conversion from char[] to char*.
string str = "hello"; str.at(2); // refer to first 'l'In D, use the usual & operator:
char[] str = "hello"; &str[2];Or, it can directly be used as an out parameter:
void foo(out char c); foo(str[2]);
switch (str) { case "hello": case "world": ... }where str can be any of literal "string"s, fixed string arrays like char[10], or dynamic strings like char[]. A quality implementation can, of course, explore many strategies of efficiently implementing this based on the contents of the case strings.
string str = "hello"; str.replace(1,2,2,'?'); // str is "h??llo"In D, use the array slicing syntax in the natural manner:
char[] str = "hello"; str[1..2] = '?'; // str is "h??llo"