String.Split only on first separator in C#?

后端 未结 4 1377
暖寄归人
暖寄归人 2020-12-08 05:59

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

4条回答
  •  被撕碎了的回忆
    2020-12-08 06:38

    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.

提交回复
热议问题