Link to home
Start Free TrialLog in
Avatar of 1dev1
1dev1

asked on

searching for a word in a string

i have a string and i want to search it to see if it contains a certain word and then get the word after it.  is anyone abe to show me how i do this

String aaa = "This string has an id 1234"

I want to see if the 'id' word exists and then get the '1234' value

The value won't always be 4 digits, sometimes more, sometimes less

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

This regex pattern in tthe sample code will do what you need.

using System.Text.RegularExpressions;
 
            String aaa = "This string has an id 1234";
            String id = "";
            Match m = Regex.Match(aaa, @"(?i)(\s+id\s+)(?<ID>\d+)");
            if (m.Success)
            {
                id = m.Groups["ID"].Value;
            }
            MessageBox.Show("ID = " + id);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sughill
sughill


// if you want to return a string that is after a substring,
        // you first need to see if it exists and use index values to return it.
        // i'm not sure why you would want to return a string AFTER a substring
        // but here you go. There are many ways to do this. here are two examples.

        public static string GetSubString(string stringToSearch, string sibling)
        {
            if (!stringToSearch.Contains(sibling))
                return string.Empty;

            int startIndex = stringToSearch.IndexOf(sibling) + (sibling.Length);
            char[] subString = stringToSearch.Substring(startIndex, stringToSearch.Length - startIndex).ToCharArray();

            string returnString = string.Empty;

            for (int i = 0; i < subString.Length; i++)
            {
                if (returnString != string.Empty && char.IsWhiteSpace(subString[i]))
                    break;

                if (!char.IsWhiteSpace(subString[i]))
                    returnString += subString[i];
            }

            return returnString;
        }

        public static string GetSubString(string stringToSearch, string sibling)
        {
            if (!stringToSearch.Contains(sibling))
                return string.Empty;

            string[] words = stringToSearch.Split(' ');

            int siblingPosition = 0;

            for (int i = 0; i < words.Length; i++)
                  {
                if (sibling == words[i])
                {
                    siblingPosition = i+1;
                    break;
                }
                  }

            return words[siblingPosition];
        }
@Goran;

Your solution would work fine if the id number was at the very end of the string. But if the string had more data after it you would return everything from the 1 to the end of the string.

If the data had been this,  the result would be "1234 and more data after the id"
"This string has an id 1234 and more data after the id"
The two methods that I provided are assuming that that what you are looking for is the word directly after the "sibling" string you are looking for. the 2nd method I suggested, you might want to check (for production code) the position of the word you are searching for so you don't get an outOfRange exception.

public static string GetSubString(string stringToSearch, string sibling)
        {
            if (!stringToSearch.Contains(sibling))
                return string.Empty;

            string[] words = stringToSearch.Split(' ');

            int siblingPosition = -1;

            for (int i = 0; i < words.Length; i++)
                  {
                if (sibling == words[i] && i != words.Length-1)
                {
                    siblingPosition = i+1;
                    break;
                }
                  }

            return siblingPosition != -1 ? words[siblingPosition] : string.Empty;
        }
@FernandoSoto

Yes, you are correct. That was my intention, though. :) My understanding was that remaining text should be returned. Now I see that the asker wanted to get the word after it. You code is much more elegant, although I believe id shouldn't be hardcoded as a search pattern. Could be the id was just an example? :)

Well, I won't be rewriting my example, since there are already working ones (and there aren't much points to split, so I wont be making a crowd). :)
Avatar of 1dev1

ASKER

Thanks for the responses

Sorry the points arn't that much, just joined and there were only 125

With real ddata, this solution did what I wanted it to do

thanks