什么是从D中的char []中删除空格的推荐方法,例如使用dmd 2.057,我有,
import std.stdio;
import std.string;
import std.algorithm;
char[] line;
int main(){
line = r"this is a line with spaces ";
line = removechars(line," ");
writeln(line);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译时,这将生成此错误:
Error: cannot implicitly convert expression ("this is a line with spaces ") of type string to char[]
Error: template std.string.removechars(S) if (isSomeString!(S)) does not match any function template declaration
Error: template std.string.removechars(S) if (isSomeString!(S)) cannot deduce template function from argument types !()(char[],string)
Run Code Online (Sandbox Code Playgroud)
在做一些谷歌搜索时,我发现类似的错误被报告为一个错误并且已经在2011年6月提交,但不确定它是指同一件事还是另一个问题.
一般来说,建议从字符串中删除某些字符并将前一个字符数组中的字符顺序排列的方法是什么?
在这种情况下返回
assert(line == "thisisalinewithspaces")
Run Code Online (Sandbox Code Playgroud)
删除空白字符后
removechars接受所有字符串类型(char [],wchar [],dchar [],string,wstring和dstring),但第二个参数必须与第一个相同.因此,如果您将char []作为第一个arg传递,则第二个arg也必须是char [].但是,你传递一个字符串:""
一个简单的解决方案是将字符串复制到char []:"".dup
removechars(line,"".dup)
这也有效:
removechars(line,['\ x20'])