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?
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With