C# String.Contains() does not work, not even string search derivatives

Asked

Viewed 395 times

1

I have a method in my code that looks for a person’s name inside a string but always returns false. Even when the name exists inside the string. What is totally inexplicable.

I tried using Contains ex:

linha.Contains(nome);

I tried using split line ex:

linhaSplit.Any(a=>a.Equals(nome))

I even tried the foreach to prove I wasn’t going crazy.

foreach(var item in linhaSplit){
   if(item == nome){
      return true;
   }else{
      return false;
   }
}

And yet, as I said earlier, even though the same values on both sides of the comparison insist on being false. What might be going on?

  • 1

    The nome inside linha are equally written (upper and lower case letters)?

  • Put real code that we can test with values where the problem is. Here’s how to make a [mcve].

  • Try the method equals line.equals(name) to see what comes back

  • 1

    Yes, I published the solution. The problem a little more complex. I have worked for a while with development but turns and moves we end up being caught by some prank of the development world. If you find it interesting read my reply and thank you for Help!

1 answer

5

My problem as simple as it may seem has taken me a while that I hope to spare you with this answer.

The great mystery about these comparisons and looking for string inside another string or whatever you want to call it, was due to a character known as Cruz Lorena or Cruz de Lorena inserir a descrição da imagem aqui. Character this "invisible" to the eyes when contained in a string inside Visual Studio.

First I discovered that there was something wrong comparing the size between the strings that were "identical" visually speaking, but distinct in their size. All I had to do was check the property Lenght of each of the strings.

From there, I copied both strings and pasted them into one MD5 encryption online to see if in fact the result of the encryption would be different and by luck pasting in the text field to be encrypted, the "invisible" character appeared.

Even after I discovered the character the only thing I couldn’t figure out is why Contains did not work since this character was next to the name, as in the example below:

var texto = "qualquer coisa relacionado à Gustavo"; //esse carácter é invisível aqui também.

texto.Contains("Gustavo"); //Sempre retornava false

So, right after I discovered the existence of this character, I tried the obvious, the .Replace()! For, when pasting it into the place indicating the char that should be replaced in the VS was something like:

texto.Replace("",""); //pois é ele ainda era invisível aqui também.

And of course VS would complain.

So I thought, because not only do I recover the alphanumeric characters that I need in this case (delete the different characters from that), then I applied the following Regex to my string.

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
texto = rgx.Replace(texto, "");

and made him disappear from the equation!

I don’t know what the chances of that happening to you, too, so obviously I gave a simpler example of what really happened to me, but this text comes through an api consumed by me, and it’s something totally unexpected by any application that has never been through so I believe. Even the fact that I have not found any post or blog talking about this case in specific and the google searches did not return any content that would help in solving my problem. And for this reason I decided to share this experience with you, I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.