从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)
在进行一些谷歌搜索时,我发现2011年6月有一个类似的错误被报告为bug and had been filed,但不确定它是指同一件事还是另一个问题。
一般情况下,建议从字符串中删除某些字符并修改前一个字符数组的字符顺序的方法是什么?
在这种情况下返回
assert(line == "thisisalinewithspaces")
删除空格字符后
答案 0 :(得分:5)
removechars接受所有字符串类型(char [],wchar [],dchar [],string,wstring和dstring),但第二个参数必须与第一个相同。因此,如果您将char []作为第一个arg传递,则第二个arg也必须是char []。但是,你传递一个字符串:“”
一个简单的解决方案是将字符串复制到char []:“”.dup
removechars(line,“”.dup)
这也有效:
removechars(line,['\ x20'])
答案 1 :(得分:3)
给removechars
一个immutable(char)[]
(这是string
别名的内容)。您还需要.dup
removechars
的结果来获取可变的char数组。
import std.stdio;
import std.string;
import std.algorithm;
char[] line;
void main()
{
auto str = r"this is a line with spaces ";
line = removechars(str," ").dup;
writeln(line);
}
答案 2 :(得分:1)
我尝试了所有但不能。现在我可以。
#include <iostream>
#include <string>
using namespace std;
void main(){
char pswd[10]="XOXO ";//this actually after i fetch from oracle
string pass="";
char passwd[10]="";
pass=pswd;
int x = pass.size(), y=0;
while(y<x)
{
if(pass[y]!=' ')
{passwd[y]=pass[y];}
y++;
}
strcpy(pswd,passwd);
cout<<pswd;
}