60

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 ?

sgarizvi
  • 16,623
  • 9
  • 64
  • 98
Narayan
  • 1,189
  • 6
  • 15
  • 33

7 Answers7

124
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
}
madster
  • 23
  • 2
  • 6
xanatos
  • 109,618
  • 12
  • 197
  • 280
22
var code = myString.Split(new [] {"code"}, StringSplitOptions.None)[1];
// code = " : -1"

You can tweak the string to split by - if you use "code : ", the second member of the returned array ([1]) will contain "-1", using your example.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
15

Simpler way (if your only keyword is "code" ) may be:

string ErrorCode = yourString.Split(new string[]{"code"}, StringSplitOptions.None).Last();
Nogard
  • 1,779
  • 2
  • 18
  • 21
10

add this code to your project

  public static class Extension {
        public static string TextAfter(this string value ,string search) {
            return  value.Substring(value.IndexOf(search) + search.Length);
        }
  }

then use

"code : string text ".TextAfter(":")
hossein sedighian
  • 1,711
  • 1
  • 13
  • 16
3

use indexOf() function

string s = "Error description, code : -1";
int index = s.indexOf("code");
if(index != -1)
{
  //DO YOUR LOGIC
  string errorCode = s.Substring(index+4);
}
asifsid88
  • 4,631
  • 20
  • 30
1
string founded = FindStringTakeX("UID:   994zxfa6q", "UID:", 9);


string FindStringTakeX(string strValue,string findKey,int take,bool ignoreWhiteSpace = true)
    {
        int index = strValue.IndexOf(findKey) + findKey.Length;

        if (index >= 0)
        {
            if (ignoreWhiteSpace)
            {
                while (strValue[index].ToString() == " ")
                {
                    index++;
                }
            }

            if(strValue.Length >= index + take)
            {
                string result = strValue.Substring(index, take);

                return result;
            }


        }

        return string.Empty;
    }
0
string originalSting = "This is my string";
string texttobesearched = "my";
string dataAfterTextTobeSearch= finalCommand.Split(new string[] { texttobesearched     }, StringSplitOptions.None).Last();
if(dataAfterTextobeSearch!=originalSting)
{
    //your action here if data is found
}
else
{
    //action if the data being searched was not found
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
sarto
  • 1
  • 1