0

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

User4
  • 1,330
  • 2
  • 15
  • 18
  • 1
    Can't you simply copy the content of the two strings instead of using an image? – Willem Van Onsem Jan 06 '15 at 05:55
  • first, you should use http://stackoverflow.com/questions/444798/case-insensitive-containsstring 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? – Nahum Jan 06 '15 at 05:55
  • 3
    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. – Croll Jan 06 '15 at 05:56
  • 2
    Have you checked the length of both string as might be possible some blank space contains any of both. – Neeraj Dubey Jan 06 '15 at 05:57
  • 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 https://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. – Scott Chamberlain Jan 06 '15 at 06:00
  • Sorry Guys, I added the strings as text. – User4 Jan 06 '15 at 06:05
  • @User4: I tested the code with the `string`s and it works. So you probably never called the method where the `if` statement is located. – Willem Van Onsem Jan 06 '15 at 06:05

2 Answers2

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

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks for demonstrating that String.Contains works. ;-) – Onots Jan 06 '15 at 06:10
  • @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. – Willem Van Onsem Jan 06 '15 at 06:11
  • Thanks for your answer @CommuSoft, It turns out my PC/Visual Studio just needed a restart. Thanks for the knowledge though. – User4 Jan 06 '15 at 06:13
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.

User4
  • 1,330
  • 2
  • 15
  • 18