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

前端 未结 6 2189
太阳男子
太阳男子 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 21:58

    try

    string X = "THIS IS AN AMAZING STRING";
    string Y = (X.IndexOf ( " " ) < 0) ? string.Empty : X.Substring (X.IndexOf ( " " )  + 1); // Y = IS AN AMAZING STRING
    

    As per comment (IF X is guaranteed to be a valid string with at least one space) a simpler version without checking etc.:

    string Y = X.Substring (X.IndexOf ( " " )  + 1); 
    

提交回复
热议问题