1

I perform a fuzzy search and I want to make sure, the program found the correct instance of a person.

For that reason, I compare the yielded "lastname firstname" to my expected "lastname firstname".

The code is relatively simple:

string expectedUserName = $"{nameSplit[0].ToLower()} {nameSplit[1].ToLower()}"; // result: "bechtold julian"
string result = O365Api.SearchUser(mail); // result: "bechtold julian"
if (result.Equals(expectedUserName))
{
    // Correct user is found
    // Never enters here
}

for the sake of testing, i also hardcoded the expected value:

userName = "bechtold julian";
mail = "julian.bechtold";
string result = O365Api.SearchUser(mail);
if (result.Equals(userName))
{
    // Correct user is found
    // never enters here
}

the result is the same when using a different comparing method:

if (result == userName)

and here the Values of result and username:
enter image description here
enter image description here

Am I missing out something? May the string from the web have special characters which are not shown?

What are the next steps to resolve the issue?

Julian Bechtold
  • 167
  • 1
  • 12
  • Perhaps one of them ends with an ASCII NUL character \0. Are the two strings the same length? If you Encoding.ASCII.GetBytes() them do they return the identical arrays? If you do a.StartsWith(b) or b.StartsWith(a) is one of them true? Thing with ascii NUL is that when you look at it eg in your screenshot, you don't see the nul or anything that represents it. In the old days (well, even nowadays.. but less often encountered) you could even have "abc\0def" with a length of 7 but it would look like "abc" in a textbox – Caius Jard Oct 11 '19 at 13:45
  • `==` and `Equals` are the same on `System.String` unless you override the == operator. switch to hex view in the debugger – Cee McSharpface Oct 11 '19 at 13:46
  • Possible duplication of https://stackoverflow.com/questions/3678792/are-string-equals-and-operator-really-same – stacktome Oct 11 '19 at 13:50
  • 1
    _May the string from the web have special characters which are not shown?_ Use ToCharArray() and edit the question with it's result. – André Sanson Oct 11 '19 at 13:50

1 Answers1

2

Visually indistinguishable whitespace characters (space, non-breaking space) and indistinguishable non-whitespace characters (Latin i and Turkish i, v and w in Swedish collations) can cause this sometimes.

Switch to hexadecimal view in the debugger to see the actual character values. Pay attention on a terminating null byte.

Depending on what it is,

  • write a utility function to normalize empirically verified special cases
  • use one of the string comparison overloads that take a CultureInfo and choose a culture that is accent insensitive, for example)

    if (result.Equals(userName, StringComparison.InvariantCulture))
    {
        /* match */
    }
    
  • Trim() the strings

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • thank you, that did the trick. you might want to include an example : if (result.Equals(userName, StringComparison.InvariantCulture)) – Julian Bechtold Oct 11 '19 at 14:18