97

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?

Community
  • 1
  • 1
Gaui
  • 8,723
  • 16
  • 64
  • 91
  • 4
    Why not just substrings to copy out the first word (using space as an index), and then simply remove the first word from the original string? – Greg Oct 18 '12 at 19:04

3 Answers3

208

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];
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 5
    In the two-argument form of `Split`, the first argument should be a *character array*, not a single character. See [String.Split Method (Char\[\], Int32)](https://msdn.microsoft.com/en-us/library/c1bs0eda(v=vs.110).aspx) on MSDN and the example shown in [this answer](http://stackoverflow.com/a/12962034/1497596) by user166390. – DavidRR Jan 11 '17 at 19:42
  • @DavidRR Correct. But even better to use the 1-arg form to split on all white characters. In the example of OP what appears to be a single space char could also be two spaces, or a TAB – Roland Sep 24 '18 at 13:44
26

Well, here is my "answer". It uses the fact that String.Split can be told hold many items it should split to (which I found lacking in the other answers):

string theString = "Some Very Large String Here";
var array = theString.Split(new [] { ' ' }, 2); // return at most 2 parts
// note: be sure to check it's not an empty array
string firstElem = array[0];
// note: be sure to check length first
string restOfArray = array[1];

This is very similar to the Substring method, just by a different means.

  • 1
    Just what I was about to suggest myself. The simplicity of splitting, but without the overhead of joining again. – Guffa Oct 18 '12 at 19:23
7

You can split and join the string, but why not use substrings? Then you only end up with one split instead of splitting the string into 5 parts and re-joining it. The end result is the same, but the substring is probably a bit faster.

string lcStart = "Some Very Large String Here";
int lnSpace = lcStart.IndexOf(' ');

if (lnSpace > -1)
{
    string lcFirst = lcStart.Substring(0, lnSpace);
    string lcRest = lcStart.Substring(lnSpace + 1);
}
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Sheridan Bulger
  • 1,214
  • 7
  • 9
  • Limiting the split to 2 would avoid the "N parts" issue - in this case both should behave nicely in `O(1)`, for some small C. –  Oct 18 '12 at 19:08
  • In my experience, [Substring](http://msdn.microsoft.com/en-us/library/system.string.substring.aspx) has caused too many ±1 errors. I have had better luck leaving the counting to the machine and keying off of significant characters. – Ryan Gates Oct 18 '12 at 19:15
  • 1
    I tested it, for 5000 iterations, substrings end up 1/1000th of a millisecond faster. It comes down to a matter of coding practice. The length of code is not much different either (with boundry checking to make sure that the split returned two entries). What matters is picking the right tool for the job, and the point I was making was splitting into five pieces then re-joining four of them is not the right tool. – Sheridan Bulger Oct 18 '12 at 19:16
  • 4
    @SheridanBulger If you want to actually find a performance difference don't increase the number of iterations, increase the size of the string. If you perform these two operations on a doctoral thesis (even a few times) you'll see a more profound difference. If you won't ever be using this on large data sets, then clearly performance isn't relevant. – Servy Oct 18 '12 at 19:18