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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
// 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(
return string.Empty;
int startIndex = stringToSearch.IndexOf(sib
char[] subString = stringToSearch.Substring(s
string returnString = string.Empty;
for (int i = 0; i < subString.Length; i++)
{
if (returnString != string.Empty && char.IsWhiteSpace(subStrin
break;
if (!char.IsWhiteSpace(subStr
returnString += subString[i];
}
return returnString;
}
public static string GetSubString(string stringToSearch, string sibling)
{
if (!stringToSearch.Contains(
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"
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;
}
public static string GetSubString(string stringToSearch, string sibling)
{
if (!stringToSearch.Contains(
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). :)
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). :)
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
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
Open in new window