Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 992
tsong99 Avatar asked Dec 01 '22 03:12

tsong99


1 Answers

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.

like image 174
rlibby Avatar answered Dec 03 '22 17:12

rlibby