Need to get a string after a "word" in a string in c#

Narayan picture Narayan · Feb 21, 2013 · Viewed 109.6k times · Source

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 is

"Error description, code : -1"

so i have to find the word code in the above string and i have to get the error code. I have seen regex but now clearly understood. Is there any simple way ?


xanatos picture xanatos · Feb 21, 2013
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