C # splits the first two in a line and then all the rest together

Well, the question can probably be formulated better. I have a line.

2008 apple micro pc computer

I want the string to split ' 'for the first 2 delimiters, and then leave it together. so he will return

2008  
apple  
micro pc computer  

This is a composed line, so it can be anything, but it is still the first 2 split, then everything else no matter how much everything else

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  
+3
source share
3 answers

Pass the second argument to indicate how many elements in the poppy can be broken. In your case, you will go through 3 so that you have the first two parts separated by a space, and the rest of the line in the third.

string myString = "2008 apple micro pc computer";
string[] parts = myString.Split(new char[] { ' ' }, 3);
+18
source

This would do it:

string s = "this is a test for something";            
string[] string_array =  s.Split(' ');
int length = string_array.Length;
string first = string_array[0];
string second = string_array[1];
string rest = "";
for (int i = 2; i < length; i++) rest = rest + string_array[i] + " ";
rest.TrimEnd();
0
  • string.LastIIndexof 2 i.e .
  • string.split
  • Point Point 2
-1
source

All Articles