How can I do String.StartsWith ignoring diacritics/accents/tildes?

Viewed 504

Title pretty much explains itself, but let's say I have this:

string str1 = "microondas";
string str2 = "micrómetro";
bool comparison1 = str1.StartsWith("micro", StringComparison.InvariantCultureIgnoreCase);
bool comparison2 = str2.StartsWith("micro", StringComparison.InvariantCultureIgnoreCase);

However, comparison2 will return false. Is there an easy way to make it so it ignores diacritics, accents and tildes (so ó becomes o, ñ becomes n)? Bonus if it's also case insensitive.

I know there's CompareOptions.IgnoreNonSpace which does what I want, but I haven't found a way to implement it in StartsWith. Any help is appreciated!

1 Answers

You could try utilizing the System.Globalization.CompareInfo.IsPrefix method, which accepts a CompareOptions enum. See the docs here.

Besides this, you can try to manually implement it for youself based on for example this answer, which deals with removing diacritics from a string.

Related