0

How I can searching some strings in C#? Well, if I have string "James Bond" and I am searching for "James" or "james" it will returning true, but if I am searching for "jame" it will return false. How I can do that? Maybe I need little specific. I need searching based on word which splitted by ' ' See above. If I am searching for jame it will return false. If I use contains when I search for jame it will also return true right?

2
  • your question is ambiguous. "How I can do that?" means what exactly? How can you make searching for "jame" return false or how can you make searching "jame" return true. Please re-ask your question. Commented Dec 9, 2012 at 5:49
  • I am already updated it. see my question again Commented Dec 9, 2012 at 5:57

7 Answers 7

2

You will have to use Regex for this. You cannot use Contains, since it won't take into consideration case-insensitivity and match whole word pattern. Use this instead:

string text = "James bond";

// this will return true  
bool result = Regex.IsMatch(text, "\\bjames\\b", RegexOptions.IgnoreCase);

// this will return false   
bool result = Regex.IsMatch(text, "\\bjame\\b", RegexOptions.IgnoreCase);

In the Regex formats, the above \bs will match an alphanumeric-to-nonalphanumeric boundary (or vice-versa). In this case, this ensures that you will be matching james or jame as a whole word and not a partial word.

Sign up to request clarification or add additional context in comments.

Comments

1

As per your question this is what you would use.

    var ex = "James Bond".ToLower(); //normalize case
    bool contains = ex.Split(' ').Any( x => x == "jame");
   //FALSE because the *word* "jame" is not found in "james bond"

Since this question is causing a lot of confusion, you have to keep in mind all of the casing involved.

var ex = "James Bond";
bool contains = ex.Contains("jame");
// FALSE because "jame" is not found in "James Bond" due to case-sensitivity

var ex = "James Bond".ToLower(); //normalize case
bool contains = ex.Contains("jame");
// TRUE because "jame" is FOUND in "james bond" due to normalized case

4 Comments

contains will be false
@Sahuagin that is what he desires
I thought he meant searching for jame giving false was a problem, but I think you're right. +1
at this point I have no idea what he meant, but I hope this can clear up some confusion. :)
1

You can use Split to separate a string into parts that are separated. Ex:

string[] items = "James Bond".Split(' ');
// items == { "James", "Bond" }

You can use ToLower to prevent case senstivity. Ex:

string lower = "James Bond".ToLower();
// lower == "james bond"

You can use StartsWith to determine if a string starts with some substring. Ex:

bool startsWithJame = "James Bond".StartsWith("Jame");
// startsWithJame == true

Using them all together:

bool anyWordStartsWith_jame_NoCaseSensitivity =
   "James Bond"
   .ToLower()
   .Split(' ')
   .Any(str => str.StartsWith("jame"));
// anyWordStartsWith_jame_NoCaseSensitivity == true

Comments

0

Use a regular expression. For an example see:

http://msdn.microsoft.com/en-us/library/ms228595%28v=vs.80%29.aspx

here is is matching when "cow" is found anywhere inside the string. In your case it would be "Jame".

2 Comments

although this is correct, for such a trivial problem he is better off just using Contains
@KuyaJohn you're right he clarified his question. the question caused a lot of confusion due to the language used.
0

you can use Contains method to search:

string[] names = new string[] { "joe", "bob", "chris" };
if(names.Contains("james")){
    //code
}

2 Comments

i think Contains doesn't really answers the question. if i have a string Hello World and i want to find the word Hell using contains, it will return true which the OP don't want. He wants to find the whole word just like what he have mentioned on the question.
as he mentioned,he wants to search a part of string for example "jame" not exactly "James" and in my case Contains is good idea.
0

You can do it with Regular Expression using word boundaries. The following code will show you how you can match using a case-insensitive search and NOT have false positives with things like "jame". It works anywhere in a given string.

    static void Main(string[] args)
    {
        string name_to_match = "James Bond";
        string word_to_find = "james";
        string word_to_not_find = "jame";

        string patternToFind = string.Format(@"\b{0}\b", word_to_find);
        string patternToNotFind = string.Format(@"\b{0}\b", word_to_not_find);

        Regex regexToFind = new Regex(patternToFind, RegexOptions.IgnoreCase);
        Regex regexToNotFind = new Regex(patternToNotFind, RegexOptions.IgnoreCase);

        bool foundNameExpected = regexToFind.IsMatch(name_to_match);
        bool foundNameNotExpected = regexToNotFind.IsMatch(name_to_match);
    }

In this case, the boolean foundNameExpected will be true (because we expect "james" to match "James" in "James Bond"), however foundNameNotExpected will be false (because we do not want "jame" to match "James").

Comments

-3

Try this out.

Method:1

string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox";
bool b;
bool c;
b = s1.Contains(s2);
c = s1.Contains("test")

bool b will return true.
bool c will return false.


Method:2

    string str = "My Name is james";  

 int result = str.IndexOf("james"); 

result will be -1 if the particular word is not found in the sentence.

4 Comments

i think Contains doesn't really answers the question. if i have a string Hello World and i want to find the word Hell using contains, it will return true which the OP don't want. He wants to find the whole word just like what he have mentioned on the question.
@KuyaJohn We can fetch the expected result by using IndexOf("") method.
@Bala Ok.. and particularly why did you choose this answer for our communication.. Err.. :)
@Bala Ok i searched for you yesterday, but you were not available. where did you go yesterday?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.