我正在尝试检查一个字符串是否包含特定字符串。
简单地说,这是我的代码(这是程序的一小部分,我省略了不相关的代码):
string y = someValue;
for(string x in someCollection)
{
if (x.Contains(y))
{
Debug.WriteLine(x + " Contains " + y);
}
else
{
Debug.WriteLine(x + " Does Not Contain " + y);
}
}
然而,这是我得到的结果:
"Alligator" Contains "Alligator"
"Loves" Does Not Contain "Love"
"To Eat You" Does Not Contain "You"
所以,怎么会!?!仅当两个字符串完全匹配时,Contains() 才返回 true??这里有些不对...
附言。字符串 x 和 y 是从文本文件中读取的,并且已经过一些文本切割过程,如果这有帮助的话......
最佳答案
您的输出是正确的。你的期望是错误的。您的困惑在于引号字符。 "Loves"
确实不包含 "Love"
,但确实包含 "Love
。
关于C# String.Contains() 故障?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5287658/