Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split and join C# string [duplicate]

Tags:

arrays

string

c#

Possible Duplicate:
First split then join a subset of a string

I'm trying to split a string into an array, take first element out (use it) and then join the rest of the array into a seperate string.

Example:

theString = "Some Very Large String Here" 

Would become:

theArray = [ "Some", "Very", "Large", "String", "Here" ] 

Then I want to set the first element in a variable and use it later on.

Then I want to join the rest of the array into a new string.

So it would become:

firstElem = "Some"; restOfArray = "Very Large String Here" 

I know I can use theArray[0] for the first element, but how would I concatinate the rest of the array to a new string?

like image 308
Gaui Avatar asked Oct 18 '12 19:10

Gaui


1 Answers

You can use string.Split and string.Join:

string theString = "Some Very Large String Here"; var array = theString.Split(' '); string firstElem = array.First(); string restOfArray = string.Join(" ", array.Skip(1)); 

If you know you always only want to split off the first element, you can use:

var array = theString.Split(' ', 2); 

This makes it so you don't have to join:

string restOfArray = array[1]; 
like image 188
Reed Copsey Avatar answered Oct 04 '22 22:10

Reed Copsey