D 如何拆分字符串数组(字符串[])?
我有一个字符串数组D 如何拆分字符串数组(字符串[])?,d,D,我有一个字符串数组 string [] foo 数组包含下一个数据: 2014 01 02 234 124 2014 01 03 640 710 2014 01 04 234 921 我需要一个新的字符串数组date,它只包括date(yyyy-MM-dd)。我该怎么做呢?这里有一个功能性方法: string[] dates = foo.map!(line => line.split()[0..3].join("-")).array(); 以下是一种功能性方法: string[] d
string [] foo
数组包含下一个数据:
2014 01 02 234 124
2014 01 03 640 710
2014 01 04 234 921
我需要一个新的字符串数组date,它只包括date(yyyy-MM-dd)。我该怎么做呢?这里有一个功能性方法:
string[] dates = foo.map!(line => line.split()[0..3].join("-")).array();
以下是一种功能性方法:
string[] dates = foo.map!(line => line.split()[0..3].join("-")).array();
如果你知道你的日期总是yyyy-MM-dd,那就不要只对字符串进行切片 翻译是标准字符串
dchar[dchar] translateTable = [' ' : '-'];
auto dates = foo.map!(line => translate(line[0..10], translateTable));
如果你知道你的日期总是yyyy-MM-dd,那就不要只对字符串进行切片 翻译是标准字符串
dchar[dchar] translateTable = [' ' : '-'];
auto dates = foo.map!(line => translate(line[0..10], translateTable));
谢谢,但如果我只是这么做,为什么:
datetime=content.split()[0..3]代码>?我收到错误:错误:模板std.array.split无法从参数类型推断函数!()(字符串[]),候选项是:
不能用每行的空格一次拆分整个字符串数组,必须依次处理每行。为此,您可以使用For
,foreach
,或者我在这里使用的map
。谢谢,但如果我只是这么做,为什么:datetime=content.split()[0..3]代码>?我收到错误:错误:模板std.array.split无法从参数类型推断函数!()(字符串[]),候选项是:
不能用每行的空格一次拆分整个字符串数组,必须依次处理每行。为此,您可以使用For
,foreach
,或者我在这里使用的map
。