Thứ Tư, 7 tháng 11, 2012

Hàm bỏ dấu tiếng việt trong C#

Bỏ dấu bằng cách replace các ký tự có dấu về ký tự không dấu tương ứng:
  1. public static string RemoveUnicode(string text)  
  2. {  
  3.     string[] arr1 = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",  
  4.     "đ",  
  5.     "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
  6.     "í","ì","ỉ","ĩ","ị",  
  7.     "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
  8.     "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
  9.     "ý","ỳ","ỷ","ỹ","ỵ",};  
  10.     string[] arr2 = new string[] { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",  
  11.     "d",  
  12.     "e","e","e","e","e","e","e","e","e","e","e",  
  13.     "i","i","i","i","i",  
  14.     "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",  
  15.     "u","u","u","u","u","u","u","u","u","u","u",  
  16.     "y","y","y","y","y",};  
  17.     for (int i = 0; i < arr1.Length; i++)  
  18.     {  
  19.         text = text.Replace(arr1[i], arr2[i]);  
  20.         text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());  
  21.     }  
  22.     return text;  
  23. }  
Viết theo kiểu Extension:
  1. public static string NonUnicode(this string text)  
  2. {  
  3.     string[] arr1 = new string[] { "á", "à", "ả", "ã", "ạ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ",  
  4.     "đ",  
  5.     "é","è","ẻ","ẽ","ẹ","ê","ế","ề","ể","ễ","ệ",  
  6.     "í","ì","ỉ","ĩ","ị",  
  7.     "ó","ò","ỏ","õ","ọ","ô","ố","ồ","ổ","ỗ","ộ","ơ","ớ","ờ","ở","ỡ","ợ",  
  8.     "ú","ù","ủ","ũ","ụ","ư","ứ","ừ","ử","ữ","ự",  
  9.     "ý","ỳ","ỷ","ỹ","ỵ",};  
  10.     string[] arr2 = new string[] { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",  
  11.     "d",  
  12.     "e","e","e","e","e","e","e","e","e","e","e",  
  13.     "i","i","i","i","i",  
  14.     "o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o","o",  
  15.     "u","u","u","u","u","u","u","u","u","u","u",  
  16.     "y","y","y","y","y",};  
  17.     for (int i = 0; i < arr1.Length; i++)  
  18.     {  
  19.         text = text.Replace(arr1[i], arr2[i]);  
  20.         text = text.Replace(arr1[i].ToUpper(), arr2[i].ToUpper());  
  21.     }  
  22.     return text;  
  23. }  
Test:
  1. string test1 = "hành trang lập trình chấm cơm".NonUnicode();  
  2. string test2 = Functions.RemoveUnicode("hành trang lập trình chấm cơm");  

3 nhận xét :

  1. Bạn có thể sử dụng Regex để viết ngắn gọn hơn.
    static Regex ConvertToUnsign_rg = null;

    public static string ConvertToUnsign(string strInput)
    {
    if (ReferenceEquals(ConvertToUnsign_rg, null))
    {
    ConvertToUnsign_rg = new Regex("\\p{IsCombiningDiacriticalMarks}+");
    }
    var temp = strInput.Normalize(NormalizationForm.FormD);
    return ConvertToUnsign_rg.Replace(temp, string.Empty).Replace("đ", "d").Replace("Đ", "D").ToLower();
    }

    https://laptrinhvb.net

    Trả lờiXóa