Last active
December 17, 2022 00:27
-
-
Save andy01pr/0cda1cc2ed948cff96da86319c69b0f1 to your computer and use it in GitHub Desktop.
How to remove or Latinize diacritics/accents in C# (.NET Framework)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
namespace LatinizeExample | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
//If using .NET Core | |
//add the Nuget library System.Text.Encoding.CodePages and then | |
//register the provider at the start of the application | |
//Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | |
Console.WriteLine("crème brûléeĆČ Leoš Janácek Dvořák".Latinize()); | |
Console.ReadLine(); | |
} | |
} | |
public static class StringExtensions | |
{ | |
private static readonly Encoding latinizeEncoding = Encoding.GetEncoding("ISO-8859-8"); | |
public static string Latinize(this string value) | |
{ | |
var strBytes = latinizeEncoding.GetBytes(value); | |
return latinizeEncoding.GetString(strBytes); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment