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?
-
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.Stan R.– Stan R.2012-12-09 05:49:51 +00:00Commented Dec 9, 2012 at 5:49
-
I am already updated it. see my question againalbilaga– albilaga2012-12-09 05:57:50 +00:00Commented Dec 9, 2012 at 5:57
7 Answers
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.
Comments
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 falsejame giving false was a problem, but I think you're right. +1You 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
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".
you can use Contains method to search:
string[] names = new string[] { "joe", "bob", "chris" };
if(names.Contains("james")){
//code
}
2 Comments
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.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
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
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.