Need to get a string after a “word” in a string in c#

前端 未结 7 1595
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 13:45

i\'m having a string in c# for which i have to find a specific word \"code\" in the string and have to get the remaining string after the word \"code\".

The string i

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 13:51

    string toBeSearched = "code : ";
    string code = myString.Substring(myString.IndexOf(toBeSearched) + toBeSearched.Length);
    

    Something like this?

    Perhaps you should handle the case of missing code :...

    string toBeSearched = "code : ";
    int ix = myString.IndexOf(toBeSearched);
    
    if (ix != -1) 
    {
        string code = myString.Substring(ix + toBeSearched.Length);
        // do something here
    }
    

提交回复
热议问题