簡體   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