4

Dのchar[]から空白を削除するための推奨される方法は何ですか。たとえば、私が持っているdmd2.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月に提出されたことがわかりましたが、それが同じものを指しているのか、別の問題を指しているのかわかりません。

一般に、文字列から特定の文字を削除し、前の文字配列の文字の順序を維持するために推奨されるアプローチは何ですか?

この場合、

assert(line == "thisisalinewithspaces")

空白文字を削除した後

4

3 に答える 3

5

removecharsは、すべての文字列型(char []、wchar []、dchar []、string、wstring、およびdstring)を受け入れますが、2番目の引数は最初の引数と同じ型である必要があります。したがって、char []を最初の引数として渡す場合、2番目の引数もchar[]である必要があります。ただし、文字列を渡しています: ""

簡単な解決策は、文字列をchar []に複製することです: "" .dup

removechars(line、 "" .dup)

これも機能します:

removechars(line、['\ x20'])

于 2012-02-06T21:54:33.647 に答える
3

(これはエイリアス化されたものです)removecharsを与えます。また、変更可能なchar配列を取得するための結果も必要になります。immutable(char)[]string.dupremovechars

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);
}
于 2012-02-06T20:56:52.093 に答える
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;
}
于 2012-12-20T15:05:30.603 に答える