Fast and clever way to get the NON FIRST segment of a C# string

前端 未结 6 2182
太阳男子
太阳男子 2021-02-18 21:22

I do a split(\' \') over an string and I want to pull the first element of the returned string in order to get the rest of the string.

f.e. \"THIS IS

6条回答
  •  梦谈多话
    2021-02-18 22:14

    A fairly good option would be to use:

    string original = "THIS IS AN AMAZING STRING";
    string[] split = original.Split(new []{' '}, 2);
    string result = split[1];
    

    Note that, if you just want the resulting string, you can shorten this:

    var result = original.Split(new []{' '}, 2)[1];
    

    By using the overload of string.Split which takes a max number of splits, you avoid the need to join as well as the extra overhead.

提交回复
热议问题