c# - 在多个数组中拆分字符串的最佳方法是什么

时间:2016-11-03 12:03:57

标签: c# arrays file

我正在使用WebRequestMethods.Ftp.ListDirectoryDetails方法从FTP服务器获取文件。

字符串格式为:11-02-16 11:33AM abc.xml\r\n11-02-16 11:35AM xyz.xml
我可以将11-02-16 11:33AM abc.xml存储在一个数组中。

如何将日期和文件名存储在数组中。

我不想枚举整个数组并再次拆分每个值。

1 个答案:

答案 0 :(得分:0)

我建议使用Dictionary<DateTime, string>;

List<string> splits = "yourSplitsStringArray".ToList();

//Create your Result Dictionary
Dictionary<DateTime, string> result = new Dictionary<DateTime, string>();

//Process your data:
splits.ForEach(x => result.Add(DateTime.Parse(x.Substring(0, 16)), x.Substring(17, x.Length - 17)));

关于你的字符串:

|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|
|1|1|-|0|2|-|1|6| |1| 1| :| 3| 3| A| M| a| b| c| .| x| m| l|

因此,您的日期时间从[0]开始,总长度为16 =&gt; x.Substring(0, 16)

您的文件名从[17]开始,并且x.Lenght - 17字符很长。

我知道你不想再次列举所有的em,但我认为这是实现你所需要的最简单实用的方法。

您还可以将我的部分答案包含在您的第一次拆分操作中。

<强> BUT:

由于它是字典,密钥DateTime必须是唯一的。 因此,如果您不确定是否会出现这种情况,请改用List<Tuple<DateTime, string>>。它类似于字典。

这会将您的Linq更改为:

//Process your data:
splits.ForEach(x => result.Add(new Tuple<DateTime, string>(DateTime.Parse(x.Substring(0, 16)), x.Substring(17, x.Length - 17))));
相关问题