简体   繁体   English

C#String.Contains()故障?

[英]C# String.Contains() malfunction?

I am trying to check whether a string contains a specific string or not. 我正在尝试检查字符串是否包含特定字符串。

Briefly, here is my code (which is a small part of the program where I omitted irrelevant codes) : 简而言之,这是我的代码(这是程序的一小部分,我省略了不相关的代码):

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);
                }
            }

However, this is what I get as the result: 但是,这就是我得到的结果:

"Alligator" Contains "Alligator"
"Loves" Does Not Contain "Love"
"To Eat You" Does Not Contain "You"

So, how come!?! 那么,怎么回事!?! Contains() returns true only when both strings are the exact matches?? Contains()仅在两个字符串都是完全匹配时返回true? Something is not right here... 这有些不对劲...

ps. PS。 the string x and y were read from a text file and have been through some text cutting process, if that would help... 字符串x和y是从文本文件中读取的,并且已经通过一些文本切割过程,如果这会有帮助...

Your output is correct. 你的输出是正确的。 Your expectation is wrong. 你的期望是错误的。 Your confusion is over the quote characters. 你的困惑在于引用字符。 "Loves" really does not contain "Love" , but it does contain "Love . "Loves"真的不包含"Love" ,但确实包含"Love

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM