简体   繁体   中英

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

I am using WebRequestMethods.Ftp.ListDirectoryDetails method to fetch files from the FTP server.

The string format is: 11-02-16 11:33AM abc.xml\\r\\n11-02-16 11:35AM xyz.xml
I am able to store 11-02-16 11:33AM abc.xml in a array.

How can i store the date and file name in an array.

I don't want to enumerate the whole array and split each value again.

I would recommend using a 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)));

Regarding your string:

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

So your DateTime starts at [0] with a total lenght of 16 => x.Substring(0, 16) .

Your filename start at [17] with and is x.Lenght - 17 chars long.

I know you don't want to enumerate all of em again, but I think it's the most simple and practical way to achieve what you need.

You could also include part of my answer into your first splitting operation.

BUT:

Since it's a Dictionary the Key which is the DateTime has to be unique. So if you aren't sure if that's gonna be the case, use List<Tuple<DateTime, string>> instead. It's similar to a Dictionary.

This will change your Linq to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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