gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:59:33 25 4
gpt4 key购买 nike

从 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)

在做一些谷歌搜索时,我发现类似的错误被报告为 bug and had been filed 2011 年 6 月,但不确定它指的是同一件事还是不同的问题。

一般来说,建议从字符串中删除某些字符并保持前一个字符数组中的字符顺序的方法是什么?

在这种情况下返回
assert(line == "thisisalinewithspaces")

删除空白字符后

最佳答案

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

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

removechars(line, " ".dup)



这也有效:

removechars(line, ['\x20'])

关于d - 从 D 中的 char[] 数组中删除空白字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9167023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com