Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
I need to strip all the symbols from a string in order to create an `IEqualityComparer` that ignores punctuation symbols

In part of my application I have an option that displays a list of albums by the current artist that aren't in the music library. To get this I call a music API to get the list of all albums by that artist and then I remove the albums that are in the current library.

To cope with the different casing of names and the possibility of missing (or extra punctuation) in the title I have written an IEqualityComparer to use in the .Except call:

var missingAlbums = allAbumns.Except(ownedAlbums, new NameComparer());

This is the Equals method:

public bool Equals(string x, string y)
{
    // Check whether the compared objects reference the same data.
    if (ReferenceEquals(x, y)) return true;

    // Check whether any of the compared objects is null.
    if (x is null || y is null)
        return false;

    return string.Compare(x, y, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0;
}

This is the GetHashCode method:

public int GetHashCode(string obj)
{
    // Check whether the object is null
    if (obj is null) return 0;

    // Make lower case. How do I strip symbols?
    return obj.ToLower().GetHashCode();
}

This fails, of course, when the string contains symbols as I'm not removing them before getting the hash code so the two strings (e.g. "Baa, baa, black sheep" and "Baa baa Black sheep") are still not equal even after converting to lower case.

I have written a method that will strip the symbols, but that meant I had to guess what those symbols actually are. It works for the cases I've tried so far, but I'm expecting it to fail eventually. I'd like a more reliable method of removing the symbols.

Given that the CompareOptions.IgnoreSymbols exists, is there a method I can call that will strip these characters from a string? Or failing that, a method that will return all the symbols?

I have found the IsPunctuation method for characters, but I can't determine whether what this deems to be punctuation is the same as what the string compare option deems to be a symbol.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you're going to use the CompareOptions enum, I feel like you might as well use it with the CompareInfo class that it's documented as being designed for:

Defines the string comparison options to use with CompareInfo.

Then you can just use the GetHashCode(string, CompareOptions) method from that class (and even the Compare(string, string, CompareOptions) method if you like).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda