从 D 中的 char[] 数组中删除空白字符
从 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;
}
在编译时,这将生成此错误:
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)
在进行一些谷歌搜索时,我发现类似的错误已报告为一个 < 一个href="http://www.digitalmars.com/d/archives/digitalmars/D/bugs/Issue_6191_New_removechars_doesn_t_accept_a_const_string_30931.html" rel="nofollow">bug 已于 2011 年 6 月提交,但不确定是否它指的是同一件事或不同的问题。
一般来说,建议使用什么方法从字符串中删除某些字符并保持先前字符数组中的字符顺序?
返回
assert(line == "thisisalinewithspaces")
在这种情况下,删除空白字符后
What is the recomended way to remove white space from a char[] in D. for example using dmd 2.057 I have,
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;
}
On compile, this will generate this error:
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)
On doing some google search, i find that a similar error had been reported as a bug and had been filed in june 2011, but not sure whether it was was referring to the same thing or a different issue.
In general, what is the approach recommended to remove certain characters from a string and maintating the order of characters from the previous array of chars?
In this case return
assert(line == "thisisalinewithspaces")
after removing whitespace characters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
removechars 接受所有字符串类型(char[]、wchar[]、dchar[]、string、wstring 和 dstring),但第二个参数必须与第一个参数的类型相同。因此,如果您传递 char[] 作为第一个参数,则第二个参数也必须是 char[]。但是,您传递的是一个字符串: " "
一个简单的解决方案是将字符串复制到 char[]: " ".dup
这也有效:
removechars accepts all string types (char[], wchar[], dchar[], string, wstring and dstring), but the second argument has to be the same type as the first. So if you pass a char[] as first arg, the second arg also has to be a char[]. You are, however, passing a string: " "
A simple solution would be to duplicate the string to a char[]: " ".dup
This also works:
给
removechars
一个immutable(char)[]
(这是string
的别名)。您还需要.dup
removechars
的结果来获取可变的 char 数组。Give
removechars
animmutable(char)[]
(which is whatstring
is aliased to). You'll also need to.dup
the result ofremovechars
to get a mutable char array.我尝试了一切,但还是不行。现在我可以了。
I try all but can't. Now I can.