Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
We are doing string search based on contains.

string str="Hello iam saritha help me about this problem".

If(str.contains( array[i])==true)
{
  
}


we are giving the string like this:

"llo".

If the word exactly like "llo" then only display the output.

But it will comes based on he"llo" last three words.

We want exact word "llo".
Posted
Updated 19-Jun-11 21:51pm
v2

You could use a regular expression[^]: quick search yields this[^].
 
Share this answer
 
String.Contains is good for exact matches, but it doesn't work too well for more complex things. You could do it by surrounding your word with spaces:
If(str.contains(string.Format(" {0} ", array[i]))==true)
But that will fail when you get punctuation.
Have you considered a Regex?
 
Share this answer
 
Comments
Member 7932936 20-Jun-11 4:28am    
It does not working.
OriginalGriff 20-Jun-11 4:45am    
As I said, it will fail if you have punctuation - do you?
Alternatively, what values are using in array[i]?
Hi
There are many ways yo can do this.
One may be
C#
String str = "Hello, Hello bssjd sbndjsjdas anasj hello.hello;";
            string[] _words=str.Split(new char[] { ' ',';',',','.'});
            var _matches = _words.Where(s => s.Equals("MatchWord", StringComparison.InvariantCultureIgnoreCase)).ToList();
 
Share this answer
 
Here is one way to do it:

C#
string str = "Hello iam saritha help me about this problem";
            string[] ss = str.Split(' ');
            foreach (string s in ss)
            {
                if (s.Equals("llo"))
                {
                     //Found "llo"
                }
            }


But the disadvantage is if the string has something like this: "llo, iam saritha", then the above technique will fail.
 
Share this answer
 
 
Share this answer
 
You can do this with regular expressions-

Code snippet-
C#
if(Regex.IsMatch(textToCheck, patternToMatch))

    //Do something
 
Share this answer
 
Comments
phil.o 10-Nov-15 5:14am    
You basically posted an incomplete solution to a four years old question.
What is your point in doing that?
Jαved 28-Dec-15 6:26am    
Hi phil,
the solution isn't incomplete, and for 4 year old question because the question dont have any accepted answer yet.
thanks,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900