当前位置: 移动技术网 > IT编程>开发语言>c# > c# 中文转拼音without CJK

c# 中文转拼音without CJK

2019年07月18日  | 移动技术网IT编程  | 我要评论
xamarin写android程序时,通常要使用按中文首字母分组显示(如通讯录) 。 于是需要被迫包含cjk,不过包含后包肯定是会变大的,于是。。。。自己写了一个硬枚举的

xamarin写android程序时,通常要使用按中文首字母分组显示(如通讯录) 。

于是需要被迫包含cjk,不过包含后包肯定是会变大的,于是。。。。自己写了一个硬枚举的中文转拼音的类。

原理是这样的:

public class pinyinutils
{
 private static readonly dictionary<string, string> pinyindict = new dictionary<string, string>
 {

 {"猿", "yuan"}
 // 等............
 };
 /// <summary>
 /// return to the first letter
 /// </summary>
 /// <param name="word">chinese word</param>
 /// <example>
 /// getfirstpinyinchar("张三")
 /// will return "z"
 /// can be used for address book index and so on
 /// </example>
 /// <returns></returns>
 public static string getfirstpinyinchar(string word)
 {
 if (word.length == 0) return "#";
 var firstletter = word[0].tostring();
 if (pinyindict.containskey(firstletter))
 {
  return pinyindict[firstletter];
 }
 return firstletter;
 }
 /// <summary>
 /// return the chinese char's pinyin
 /// </summary>
 /// <param name="chinesechar"></param>
 /// <example>
 /// getpinyin('福')
 /// will return "fu"
 /// </example>
 /// <returns></returns>
 public static string getpinyin(char chinesechar)
 {
 var str = chinesechar.tostring();
 if (pinyindict.containskey(str))
 {
  return pinyindict[str];
 }
 return null;
 }
 /// <summary>
 /// get the phonetic abbreviation for chinese char
 /// </summary>
 /// <param name="chinesechar"></param>
 /// <example>
 /// getshortpinyin('福')
 /// will return "f"
 /// </example>
 /// <returns></returns>
 public static string getshortpinyin(char chinesechar)
 {
 var str = chinesechar.tostring();
 if (pinyindict.containskey(str))
 {
  var first = pinyindict[str].firstordefault();
  if (first == 0) return null;
  return first.tostring();
 }
 return null;
 }
}

源码:

https://github.com/chsword/pinyinutil/blob/master/pinyinutils.cs

github:https://github.com/chsword/pinyinutil

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网