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

标签 d dmd phobos

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

相关文章:

将 D 项目编译为库 - 依赖项会发生什么情况?

visual-studio - 为什么这个 D 程序会抛出奇怪的访问冲突异常?

gcc - 如何在 Ubuntu 上使用 DSFML2 和 D2 解决链接器错误?

namespaces - 如何解决 d 中的 "conflicts with"错误?

posix - 加载共享库时自动执行的函数

linux - 在 linux 下运行 dmd-tango 的问题

d - DMD 与 GDC 中的 memcmp 和标准并行性 : parallel

casting - D中的整数到字符串转换

d - 我应该使用 Phobos 还是 Tango?

sockets - D套接字编程基本连接脚本