Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# split first two in string, then keep all the rest together

Tags:

c#

Okay, the question could probably a phrased better. I have a string.

2008 apple micro pc computer

i want the string split by ' ' for the first 2 delimiters and then keep the rest together. so it'll return

2008  
apple  
micro pc computer  

This is a made up string so it could be anything, but it's still first 2 split, then all the rest no matter how much is all the rest

Another example

 apple orange this is the rest of my string and its so long  

returns

apple  
orange  
this is the rest of my string and its so long  

1 Answers

Pass a second argument to specify how many items at max to split into. In your case, you'd pass 3 so you have the first two parts split by space, and the rest of the string in the third.

string myString = "2008 apple micro pc computer";
string[] parts = myString.Split(new char[] { ' ' }, 3);
like image 65
BoltClock Avatar answered Sep 13 '25 11:09

BoltClock