Answered by:
how to Search string for the exact word c#

Question
-
Hello Everybody,
I have string s= "Hello1 Hello2";
s.contains("Hello"); will return me true. but i want to see if the string contains the exact word "Hello" which it does not.
how can i do that?
Thanks,
Jan :)
Wednesday, April 29, 2009 1:33 PM
Answers
-
U can use a regular expression like so:
bool contains = Regex.IsMatch("Hello1 Hello2", @"\bHello\b"); // yields false bool contains = Regex.IsMatch("Hello1 Hello", @"\bHello\b"); // yields true
the \b is a word boundary check, and used like above it will be able to match whole words only.- Edited by M. Dirksma Wednesday, April 29, 2009 1:53 PM
- Marked as answer by CodeSri Wednesday, April 29, 2009 1:54 PM
Wednesday, April 29, 2009 1:44 PM
All replies
-
U can use a regular expression like so:
bool contains = Regex.IsMatch("Hello1 Hello2", @"\bHello\b"); // yields false bool contains = Regex.IsMatch("Hello1 Hello", @"\bHello\b"); // yields true
the \b is a word boundary check, and used like above it will be able to match whole words only.- Edited by M. Dirksma Wednesday, April 29, 2009 1:53 PM
- Marked as answer by CodeSri Wednesday, April 29, 2009 1:54 PM
Wednesday, April 29, 2009 1:44 PM -
I would suggest using Regex for this. By appending the \b to either side of the text you're trying to match, you can find the "exact" match within the text.
Regex.IsMatch("Hello1 Hello2", "\bHello\b");
Something like this method below should work for you every time:static bool ExactMatch(string input, string match)
{
return Regex.IsMatch(input, string.Format(@"\b{0}\b", Regex.Escape(match)));
}
David Morton - http://blog.davemorton.net/- Proposed as answer by M. Dirksma Wednesday, April 29, 2009 2:04 PM
Wednesday, April 29, 2009 1:45 PM -
thanks guys :)
that was fast :)Wednesday, April 29, 2009 1:54 PM -
Apparently the Regex does not consider the colon, so even if I write 'Hello:', true will still be returned. How can I fix that?Tuesday, November 9, 2010 12:21 PM
-
Im pretty sure you can test for "Hello " which will search for the space aswell.Thursday, May 11, 2017 12:25 PM
-
Apparently the Regex does not consider the colon, so even if I write 'Hello:', true will still be returned. How can I fix that?
Well it's a matter of opinion if the colon is relevant or not, it depends upon the specific problem and your's is just one of a host of possible scenarios.
You'd need to define each case that you consider legal and then code for it, we cannot be expected to know that "Hello " is valid yet "Hello:" is not - unless you list this out.
If all you will accept is:
"Hello"
"Hello "
" Hello"
" Hello "
then the code is trivial:
bool Contains(string Text, string Word) { if (Text == Word) return true; if (Text.EndsWith(" " + Word) || Text.BeginsWith(Word + " ")) return true; if (Text.Contains(" " + Word + " ")) return true; }
- Proposed as answer by Chan77569 Monday, October 30, 2017 7:14 PM
Monday, June 19, 2017 10:50 PM -
Work as expected, ThanksMonday, October 30, 2017 7:14 PM