简体   繁体   中英

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?? 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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