Zai*_*pai 0 c# regex string cpu-word
让我们举一个英语句子的例子:
string sentence = "Hello, How are you?";
Run Code Online (Sandbox Code Playgroud)
因此,如果我想搜索单词“ you”,则可以使用:
if (sentence.Contains("you")); // This would be true
Run Code Online (Sandbox Code Playgroud)
但是如果我搜索这个:
if (sentence.Contains("ello")); // This would also be true
Run Code Online (Sandbox Code Playgroud)
但我希望它是错误的。我想搜索整个单词。不是文字的一部分
如何在C#中执行此操作?
您可以使用正则表达式执行此操作。
var search = "ello";
string sentence = "Hello, How are you?";
string pattern = string.Format(@"\b{0}\b", search);
if(Regex.IsMatch(sentence, pattern))
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |