Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
contains not giving proper result for special charcters

C#
var searchstring = "?/!@#$$%^&*";


bool isworking = searchstring.Contains("?/!@#$$%^&*");





i am getting isworking as false
Posted
Updated 22-Jul-15 4:40am
v2
Comments
Andy Lanng 22-Jul-15 10:40am    
Works for me.

Is this the only code involved? I literally copy+pasted the first 2 lines and isworking == true
Member 11855350 23-Jul-15 2:36am    
but i am getting false

1 solution

It's possible that the code you show is not what you mean: Contains matches the whole string as-is as a substring in the original - so if the input string does not contain every character in the "match string" in exactly the same order and with no intervening characters, the match will return false.
So
C#
"ABCD".Contains("BC")
will return true, while
C#
"AB.CD".Contains("BC")
will return false.

string.Contains does not support wildcards, so if you want that you will need a Regex, and if you want "contains any of these characters" instead of a string match then you want IndexOfAny[^]
 
Share this answer
 
Comments
Member 11855350 23-Jul-15 2:32am    
in contains i am using same string but returns false ,like

var a = "?/!@#$$%^&*";

var b =a;

bool isworking = a.contains(b);

i am getting isworking as false
OriginalGriff 23-Jul-15 3:28am    
I don't think so.
I copy your code from this message into an app.
I correct "contains" to "Contains" so it compiles.
I run it.
isworking is true.

So: copy and paste the exact code you are using. It isn't what you are showing us!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900