开发者

C# String.Contains() malfunction?

开发者 https://www.devze.com 2023-02-16 06:52 出处:网络
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) :

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?? Something is not right here...

ps. the string x and y were read from a text file and have been through some text cutting process, if that would help...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号