Search code examples
c#string

Can not get .Contains to work in c#


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


Solution

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