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

1

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

I tried using Contains ex:

linha.Contains(nome);

I tried to use the dotted line ex:

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

I even tried the foreach to prove that I was not going crazy.

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

and yet, as I said earlier, even though I have identical values on both sides of the comparison, the result insists on being false. What could be happening?

    
asked by anonymous 28.03.2017 / 21:15

1 answer

5

My problem, as simple as it may seem, has taken me some time to hopefully spare you with this answer.

The great mystery about these comparisons and the search for a string within another string or whatever you want to call it was based on a character known as Lorraine Cross or Lorraine Cross .Characterthis"invisible" to the eye when contained in a string within Visual Studio.

I first discovered that there was something wrong comparing the size between strings that were "identical" visually speaking but distinct in size. For this I just needed to check the Lenght property of each of the strings.

Thereafter, I copied both strings and pasted it into an MD5 encryptor online to see if in fact the encryption result would be different and luckily when paste in the text field to be encrypted, the "invisible" character appeared.

Even after I discovered the character, the only thing I could not understand is the reason 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

Then, soon after discovering the existence of this character, I tried the obvious, .Replace () ! Because when you paste it into the place that indicates the char to be replaced in VS it looks something like:

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

Of course, VS was going to complain.

Then I thought, because not only did I retrieve the alphanumeric characters which is what I need in this case (delete the characters other than that), then I applied the following Regex in my string.

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

and made him disappear from the equation!

I do not know what the chances of this happening to you either, of course I gave a simpler example of what actually happened to me, but this text comes through an api consumed by me, and it is totally unexpected by any application that ever I've been through that, I believe. Even for the fact that I have not found any post or blog talking about this specific case and the google searches do 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!

    
28.03.2017 / 21:44