简体   繁体   中英

Split string into two arrays C#

I'd like to split a string into two arrays:

string foo = "apple;carrot";

I want to put "apple" into one array, and "carrot" in another array.

Just using foo.Split(;) would result in both words being put into the same array, one after each other.

I hope I made myself clear, and thanks in advance.

You would need to project the array to a new array for each item:

string foo = "apple;carrot";
var collection = foo.Split(';').Select(x=> new String[] { x });

This will return you IEnumerable<String[]> which you can iterate.

foreach(var array in collection)
{
  // do something with array
}

Or you can create an array of arrays like this:

var arrays = collection.ToArray();
        var splits = foo.Split( new char[]{ ';' });

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