如何在 C# 中搜索一些字符串?好吧,如果我有字符串“詹姆斯邦德”并且我正在搜索“詹姆斯”或“詹姆斯”,它将返回真,但如果我正在搜索“詹姆斯”,它将返回假。我怎么能这样做?也许我需要一点具体。我需要根据以“”分隔的单词进行搜索,请参见上文。如果我正在搜索 jame,它将返回 false。如果我在搜索 jame 时使用 contains,它也会返回 true 对吗?
7 回答
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.
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
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
使用正则表达式。有关示例,请参见:
http://msdn.microsoft.com/en-us/library/ms228595%28v=vs.80%29.aspx
当在字符串中的任何位置找到“cow”时,这里是匹配的。在您的情况下,它将是“Jame”。
您可以使用Contains
方法搜索:
string[] names = new string[] { "joe", "bob", "chris" };
if(names.Contains("james")){
//code
}
您可以使用正则表达式使用单词边界来做到这一点。以下代码将向您展示如何使用不区分大小写的搜索进行匹配,并且不会出现“jame”之类的误报。它适用于给定字符串中的任何位置。
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);
}
在这种情况下,布尔值 foundNameExpected 将为真(因为我们希望 "james" 匹配 "James Bond" 中的 "James"),但 foundNameNotExpected 将为假(因为我们不希望 "jame" 匹配 "James")。
试试这个。
方法: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 将返回 true。
bool c 将返回 false。
方法:2
string str = "My Name is james";
int result = str.IndexOf("james");
如果在句子中找不到特定的单词,结果将为 -1。