从 D 中的 char[] 数组中删除空白字符

发布于 2025-01-03 14:50:03 字数 1073 浏览 0 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼中杀气 2025-01-10 14:50:03

removechars 接受所有字符串类型(char[]、wchar[]、dchar[]、string、wstring 和 dstring),但第二个参数必须与第一个参数的类型相同。因此,如果您传递 char[] 作为第一个参数,则第二个参数也必须是 char[]。但是,您传递的是一个字符串: " "

一个简单的解决方案是将字符串复制到 char[]: " ".dup

删除字符(行,“”.dup)

这也有效:

删除字符(行,['\x20'])

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

removechars(line, " ".dup)

This also works:

removechars(line, ['\x20'])

半葬歌 2025-01-10 14:50:03

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);
}

Give removechars an immutable(char)[] (which is what string is aliased to). You'll also need to .dup the result of removechars to get a mutable char array.

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);
}
梦魇绽荼蘼 2025-01-10 14:50:03

我尝试了一切,但还是不行。现在我可以了。

#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;
}

I try all but can't. Now I can.

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文