82

String.Split is convenient for splitting a string with in multiple part on a delimiter.

How should I go on splitting a string only on the first delimiter. E.g. I've a String

"Time: 10:12:12\r\n"

And I'd want an array looking like

{"Time","10:12:12\r\n"}
3
  • split using whitespace? and set an arry = yourString.Split(' '); Commented Aug 2, 2017 at 5:21
  • 1
    @RamgyBorja that won't work unless he also trims the : from Time and anyway, not what was asked Commented Jan 11, 2018 at 19:52
  • @smurtagh yes, for safe coding used trim Commented Jan 13, 2018 at 4:24

5 Answers 5

151

The best approach depends a little on how flexible you want the parsing to be, with regard to possible extra spaces and such. Check the exact format specifications to see what you need.

yourString.Split(new char[] { ':' }, 2)

Will limit you two 2 substrings. However, this does not trim the space at the beginning of the second string. You could do that in a second operation after the split however.

yourString.Split(new char[] { ':', ' ' }, 2,
    StringSplitOptions.RemoveEmptyEntries)

Should work, but will break if you're trying to split a header name that contains a space.

yourString.Split(new string[] { ": " }, 2,
    StringSplitOptions.None);

Will do exactly what you describe, but actually requires the space to be present.

yourString.Split(new string[] { ": ", ":" }, 2,
    StringSplitOptions.None);

Makes the space optional, but you'd still have to TrimStart() in case of more than one space.

To keep the format somewhat flexible, and your code readable, I suggest using the first option:

string[] split = yourString.Split(new char[] { ':' }, 2);
// Optionally check split.Length here
split[1] = split[1].TrimStart();
Sign up to request clarification or add additional context in comments.

4 Comments

Why not use the (String[], Int32, StringSplitOptions) overload? (msdn.microsoft.com/en-us/library/1bwe3zdy.aspx). This only applies to .NET 2.0 and above, but the following gives exactly the result the OP was asking for: yourString.Split(new string[] { ": " }, 2, StringSplitOptions.None)
@Bernhof: it does. I'm not sure what should happen in case of extra spaces (or no spaces) in the string however. The best approach differs a bit depending on the desired behavior.
I find it very confusing that something like yourString.Split(':', 2); does not work... Thanks for your explanations!
.Select(s=> s.Trim()) is how I often trim the results of Split
14

In your example above you could split on ": " (i.e. colon with trailing space) as this appears to be what you've done. If you really did split on just the first delimeter you'd see a leading space in your second array element.

However, you should probably look at this overload of Split...

http://msdn.microsoft.com/en-us/library/c1bs0eda.aspx

public string[] Split(
  char[] separator,
  int count
)

... which allows you to specify a max number of substrings.

Comments

3
?("Time: 10:12:12\r\n").Split(new char[] { ':', ' ' }, 2, 
   StringSplitOptions.RemoveEmptyEntries)
{Dimensions:[2]}
    [0]: "Time"
    [1]: "10:12:12\r\n"

other options:

?("Time: 10:12:12\r\n").Split(new char[] { ':' }, 2)
{Dimensions:[2]}
    [0]: "Time"
    [1]: " 10:12:12\r\n"
?("Time: 10:12:12\r\n").Split(new char[] { ':' }, 1)
{Dimensions:[1]}
    [0]: "Time: 10:12:12\r\n"
?("Time: 10:12:12\r\n").Split(new char[] { ':' }, 3)
{Dimensions:[3]}
    [0]: "Time"
    [1]: " 10"
    [2]: "12:12\r\n"

1 Comment

The delimeter is colon, not space. You're right that splitting on space would be fine and give 2 elements, but I don't think that's the question being asked.
0

I've adopted a variation to Thorarin's answer above, The below should be able to handle your requirement, plus trim the spaces.

yourString.Split(new []{'-'},2).Select(s => s.Trim())

Comments

0

For .NET 5 upwards you can use this:

string[] split = yourString.Split(new[] {':'}, 2, StringSplitOptions.TrimEntries);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.