1

How can I do a indexOf Case Insensite with special chars? I need to search for the word "benefícios", and I want IndexOf to find it even if I don't use the special char on "i"

This way I can't find it:

string str = "candidatar-se a benefícios";
string word = "beneficio";
str.IndexOf(word, StringComparison.OrdinalIgnoreCase)

But with this one I can:

string str = "candidatar-se a benefícios exclusivos";
string word = "benefício";
str.IndexOf(word, StringComparison.OrdinalIgnoreCase)

Thanks

Kup
  • 882
  • 14
  • 31
  • Convert the special characters in the source string to your desired charset (ASCII?) *before* searching with `IndexOf`. – zzzzBov May 08 '15 at 15:00

3 Answers3

5

Use the CompareInfo of the relevant culture and use CompareOptions.IgnoreNonSpace:

string str = "candidatar-se a benefícios";
string word = "beneficio";

var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
compareInfo.IndexOf(str, word, CompareOptions.IgnoreNonSpace);

You can also combine other compare options - if you wanted to ignore case as well, for example, use CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase.

The documentation for IndexOf and CompareOptions may be of some help.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
2

Take a look at this question

The second answer seems to be exactly what you are looking for.

Community
  • 1
  • 1
Vlad274
  • 6,514
  • 2
  • 32
  • 44
1

you can have it two ways:

either you use CharUnicodeInfo.GetUnicodeCategory for diacritic marks:

static string RemoveDiacritics(string text) 
{
    var normalizedString = text.Normalize(NormalizationForm.FormD);
    var stringBuilder = new StringBuilder();

    foreach (var c in normalizedString)
    {
        var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
        if (unicodeCategory != UnicodeCategory.NonSpacingMark)
        {
            stringBuilder.Append(c);
        }
    }

    return stringBuilder.ToString().Normalize(NormalizationForm.FormC);

}

or you use the UnidecodeSharpFork https://bitbucket.org/DimaStefantsov/unidecodesharpfork

   var result = "Test æ".Unidecode();
   Console.WriteLine(result) // Prints Test ae
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41