Skip to content

Instantly share code, notes, and snippets.

@quangnle
Created April 5, 2016 10:08
Show Gist options
  • Save quangnle/64a0ed727d91a85d9b3b0291ec3682fd to your computer and use it in GitHub Desktop.
Save quangnle/64a0ed727d91a85d9b3b0291ec3682fd to your computer and use it in GitHub Desktop.
public class CharMapper
{
private static char[][] _dictionary = new char[14][]{ new char[] {'a', 'á', 'à', 'ả', 'ã', 'ạ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ă', 'ắ', 'ằ', 'ả', 'ẵ', 'ặ'},
new char[] {'d', 'đ'},
new char[] {'e', 'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ'},
new char[] {'i', 'í', 'ì', 'ỉ', 'ĩ', 'ị'},
new char[] {'o', 'ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ'},
new char[] {'u', 'ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự'},
new char[] {'y', 'ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ'},
new char[] {'A', 'Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Ă', 'Ắ', 'Ằ', 'Ả', 'Ẵ', 'Ặ'},
new char[] {'D', 'Đ'},
new char[] {'E', 'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ'},
new char[] {'I', 'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'},
new char[] {'O', 'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ'},
new char[] {'U', 'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự'},
new char[] {'Y', 'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'}};
public static string GetUnsignedString(string st)
{
if (string.IsNullOrWhiteSpace(st)) return st;
var result = "";
for (int i = 0; i < st.Length; i++)
{
char c = st[i];
var found = _dictionary.FirstOrDefault(lc => lc.Where(cc => cc == c).Count() != 0);
if (found != null)
result += found[0];
else
result += st[i];
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment