繁体   English   中英

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

[英]c# - What is the best way to split string in multiple arrays

我正在使用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存储在一个数组中。

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

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

我建议使用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|

所以你的 DateTime 从[0]开始,总长度为16 => x.Substring(0, 16)

您的文件名以[17]开头,并且是x.Lenght - 17字符长。

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

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

但:

由于它是字典,因此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))));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM