你在你的代码做了一个简单的错误:
str.replace( “我”, “A”);
str.replace
回报新的字符串做替换,它实际上并没有取代现有的变量。因此请尝试str = str.replace("i", "a");
以查看更改。
但你还就分配一个过于宽泛笼统的说法:所以对字符串处理的每个操作的内存分配
。
这是错误的,很多操作不需要分配新的内存。可以切片现有字符串任何事情都会这么做,避免了需要新的内存:
import std.string;
import std.stdio;
void main() {
string a = " foo ";
string b = a.strip();
assert(b == "foo"); // whitespace stripped off...
writeln(a.ptr);
writeln(b.ptr); // but notice how close those ptrs are
assert(b.ptr == a.ptr + 2); // yes, b is a slice of a
}
replace
也将返回如果没有更换,实际上完成了原始字符串:
string a = " foo ";
string b = a.replace("p", "a"); // there is no p to replace
assert(a.ptr is b.ptr); // so same string returned
索引和迭代不需要新分配(当然)。相信与否,但即使追加有时也不会分配,因为可能在片的末尾留下了尚未使用的内存(虽然通常会)。
还有各种函数会返回范围对象,这些对象在您遍历它们时执行更改,从而避免分配。例如,您可以使用filter!(ch => ch != 'f')(a);
之类的代替replace(a, "f", "");
的循环,除非您提出要求,否则不会分配新的字符串。
所以它比你想象的要多得多!
'string * str_ptr; str_ptr = &str; writeln(str_ptr);''''和'writeln(str.ptr);' –
'&str'是指向ARRAY的指针,'str.ptr'是指向CONTENTS的指针。 –