Remove Diacritics - CSharp System

CSharp examples for System:String Strip

Description

Remove Diacritics

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Generic;
using System;//from   w  w  w. j av a 2 s  . c  o  m

public class Main{
        /// <summary>
        /// Removes diacritics, such as the umlaut (? => e).  Though not linguistically
        /// correct it does allow UTF8 to be represented in ASCII, at least to a degree
        /// that will alert translators that further translation is required.
        /// </summary>
        /// <param name="s">String containing diacritics</param>
        /// <returns>Normalized s</returns>
        public static string RemoveDiacritics(this string s) {
            string normalizedString = s.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            foreach (char t in normalizedString.Where(t => CharUnicodeInfo.GetUnicodeCategory(t) != UnicodeCategory.NonSpacingMark)) {
                stringBuilder.Append(t);
            }

            return stringBuilder.ToString();
        }
}

Related Tutorials