Can not get .Contains to work in c#
Asked Answered
C

3

1

I've been searching and trying various things for hours and can not get this simple contains function to work.

if (department.ToLower().Contains(item2.Title.ToLower()))

Here is an image of the two strings. I've copied them to notepad to compare them and they're identical. enter image description here

Thanks for any advice you might have.

Here are the two string in text, copied straigth from visual studio debugger:

String 1 : "Shared Services - Technology and Information Services"
String 2 : "Shared Services - Technology and Information Services"

Edit - Added strings in text

Couturier answered 6/1, 2015 at 5:53 Comment(7)
Can't you simply copy the content of the two strings instead of using an image?Quotation
first, you should use #445298 second this code should work. you almost surely got some kind of invisible symbol. do they take their value from the same const string? try to do .Count are they same size?Mouthwash
This code should work. You should re-check other lines in your source. Use debugger and F10 to step from breakpoint and see how it works.. And try Trim() for both strings.Compulsory
Have you checked the length of both string as might be possible some blank space contains any of both.Laywoman
Please copy and paste the two strings in to your question. Its hard to tell from the screenshot but it appears the first one uses a hyphen - and the second uses somthing longer like a En Dash (alt + 0150) Also if you could make a runnable example of the code not working on dotnetfiddle.net it would help us figure out your problem greatly. As of right now your question could be closed as off topic for not including any kind of runable example.Emlen
Sorry Guys, I added the strings as text.Couturier
@User4: I tested the code with the strings and it works. So you probably never called the method where the if statement is located.Quotation
Q
3

The code should work (based on the csharp interactive shell):

$ csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> var dep="Shared Services - Technology and Information Services";
csharp> var oth="Shared Services - Technology and Information Services"; 
csharp> dep.ToLower().Contains(oth.ToLower())               
true

Are you sure the code with the if statement is executed, perhaps you should copy the values here, because there might be a small difference (a space for instance).

Based on the image it seems there are two spaces after the dash (-) in the department string. But this can be a trick of the font. But in general it's very bad to use an image instead of providing raw text data (that can be copied and processed).

Quotation answered 6/1, 2015 at 5:57 Comment(3)
Thanks for demonstrating that String.Contains works. ;-)Manganese
@Onots: well it is in combination with string.ToLower, it's indeed rather pointless, but it proves, the problem is not with the if statement, but with what's underneath that, or with the caller :s.Quotation
Thanks for your answer @CommuSoft, It turns out my PC/Visual Studio just needed a restart. Thanks for the knowledge though.Couturier
C
0

Looks like it's just been a visual studio compiler bug. I've just ended up restarting my PC and it seems to be working fine now.

Couturier answered 6/1, 2015 at 6:14 Comment(0)
A
-1

This is not 100% related to the OP's question but it is related and I wanted to help anyone else that may stumble on this like I did in .Net 8.

This code will not work:

var first = request.Headers.TryGetValue("Content-Type", out var contentType);
var second = contentType.Contains("application/xml") || contentType.Contains("text/xml");
return first && second;

the variable second will always be false even though when I inspect the contentType of the request header it clearly shows "application/xml; charset=utf-8" as the value.

Apparently the .Contains method doesn't work the way I expected on a StringValues object which is what the variable contentType is. I just assumed the contentType variable was a string and I spent hours troubleshooting this!

So in short, convert the StringValues object to a string and then it works fine.

Asbestos answered 27/8 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.