Reputation: 439
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?
Upvotes: 0
Views: 180
Reputation: 3385
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 \b
s 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.
Upvotes: 2
Reputation: 13158
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
Upvotes: 1
Reputation: 16065
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
Upvotes: 1
Reputation: 67207
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.
Upvotes: -3
Reputation: 326
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").
Upvotes: 0
Reputation: 10824
you can use Contains
method to search:
string[] names = new string[] { "joe", "bob", "chris" };
if(names.Contains("james")){
//code
}
Upvotes: 0
Reputation: 144
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".
Upvotes: 0