67

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 ?

3
  • 1
    What would happen should code appear twice in said string? Commented Feb 21, 2013 at 9:28
  • 1
    Can you clarify - what's the issue with regex? What have you tried and what code do you currently have? Commented Feb 21, 2013 at 9:32
  • @LukeHennerley code may appear twice or more, but at the end, the error code definition will be code: error code
    – Narayan
    Commented Feb 21, 2013 at 9:33

7 Answers 7

128
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
}
23
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.

4
  • Split only takes params[] char, it doesn't take params[] string :) Commented Feb 21, 2013 at 9:29
  • 3
    @LukeHennerley - Really?
    – Oded
    Commented Feb 21, 2013 at 9:30
  • 1
    I was meant to say this doesn't compile :P I stand corrected! Commented Feb 21, 2013 at 9:31
  • 1
    @LukeHennerley - The simplest overload only takes params char[]. All the string[] overloads require additional parameters.
    – Oded
    Commented Feb 21, 2013 at 9:33
15

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

string ErrorCode = yourString.Split(new string[]{"code"}, StringSplitOptions.None).Last();
1
  • @LukeHennerley it is very common approach for initializing array of strings
    – Nogard
    Commented Feb 21, 2013 at 9:32
11

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(":")
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);
}
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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.