从D中的char[]数组中删除空白字符
建议使用什么方法从D中的字符[]中删除空白。例如,使用dmd 2.057从D中的char[]数组中删除空白字符,d,dmd,phobos,D,Dmd,Phobos,建议使用什么方法从D中的字符[]中删除空白。例如,使用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; } 编译时,这将生成以下错误:
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月有一个类似的错误被报告为a,但不确定它是指同一件事还是不同的问题
一般来说,建议采用什么方法从字符串中删除某些字符,并维护前一个字符数组中的字符顺序
在这种情况下,返回
assert(line == "thisisalinewithspaces")
删除空白字符后给
removechars
一个不可变(char)[
(这就是字符串
的别名)。您还需要.dup
删除字符的结果来获得可变字符数组
导入标准stdio;
输入标准字符串;
导入标准算法;
字符[]行;
void main()
{
auto str=r“这是一条带空格的线”;
line=removechars(str,“”).dup;
写(行);
}
removechars接受所有字符串类型(char[]、wchar[]、dchar[]、string、wstring和dstring),但第二个参数必须与第一个参数的类型相同。因此,如果将char[]作为第一个参数传递,则第二个参数也必须是char[]。但是,您正在传递一个字符串:“
一个简单的解决方案是将字符串复制到char[]:“”.dup
删除命令(行“.dup”)
这也适用于:
删除字符(第['\x20']行)
我都试过了,但不行。现在我可以了
#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;
}
#包括
#包括
使用名称空间std;
void main(){
char pswd[10]=“XOXO”;//这实际上是在我从oracle获取之后
字符串pass=“”;
char passwd[10]=“”;
通过=pswd;
int x=pass.size(),y=0;
while(yDoes将line=r“这是一个带空格的行”;
更改为line=r“这是一个带空格的行”.dup;
帮助?不,它没有。它只消除了类型转换中的第一个错误。这也回答了我这里发生的问题:).我快速查看了removechars
的源代码,不知道为什么它不能像他写的那样工作。