39

I have two strings:

string1  = "theater is small"; 
string2 =  "The small thing in the world";

I need to check weather the string "the" is present in the strings or not.
I can use the contains function, but can it do a whole word match? i.e it should not match with "theater" of string1!

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
siva
  • 1,135
  • 4
  • 17
  • 25

6 Answers6

71

The simplest solution is to use regular expressions and the word boundary delimiter \b:

bool result = Regex.IsMatch(text, "\\bthe\\b");

or, if you want to find mismatching capitalisation,

bool result = Regex.IsMatch(text, "\\bthe\\b", RegexOptions.IgnoreCase);

(using System.Text.RegularExpressons.)

Alternatively, you can split your text into individual words and search the resulting array. However, this isn’t always trivial because it’s not enough to split on white spaces; this would ignore all punctuation and yield wrong results. A solution is to once again use regular expressions, namely Regex.Split.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
14

Use the method Regex.IsMatch using \bthe\b, \b represents a word boundary delimiter.

// false
bool string1Matched = Regex.IsMatch(string1, @"\bthe\b", RegexOptions.IgnoreCase); 

// true
bool string2Matched = Regex.IsMatch(string2, @"\bthe\b", RegexOptions.IgnoreCase); 
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
6
str.Split().Contains(word);

or

char[] separators = { '\n', ',', '.', ' ' };    // add your own
str.Split(separators).Contains(word);
Grozz
  • 8,317
  • 4
  • 38
  • 53
0

Using the answers here i made this extension method that finds multiple words in a text, returns the amount of words found, and ignores case matching.

public static int Search(this String text, params string[] pValores)
{
    int _ret = 0;
    try
    {
        var Palabras = text.Split(new char[] { ' ', '.', '?', ',', '!', '-', '(', ')', '"', '\''  }, 
            StringSplitOptions.RemoveEmptyEntries);

        foreach (string word in Palabras)
        {
            foreach (string palabra in pValores)
            {
                if (Regex.IsMatch(word, string.Format(@"\b{0}\b", palabra), RegexOptions.IgnoreCase))
                {
                    _ret++;
                }
            }
        }               
    }
    catch { }
    return _ret;
}

Usage:

string Text = @"'Oh, you can't help that,' (said the Cat) 'we're all mad here. I'm MAD. ""You"" are mad.'";
int matches = Text.Search("cat", "mad"); //<- Returns 4

It's not perfect but it works.

Jhollman
  • 2,093
  • 24
  • 19
-1

you can if you add the space in the word that you examine

Amjad Abdelrahman
  • 3,372
  • 1
  • 13
  • 20
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Black Sheep Jul 22 '14 at 12:39
  • that will often work but not if, for example, you search the string for "the " and the string *ends* with "the" as in: "this won't find the" – mtijn Jul 22 '14 at 12:50
  • @aldanux sorry but this *is* an answer to the question, just not a very good one and also a very late one... – mtijn Jul 22 '14 at 12:54
-2

You could use a regular expression instead. That way you can specify that you only want space or end of line in the end.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137