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 == ) return "#";
var firstLetter = word[].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 == ) return null;
return first.ToString();
}
return null;
} }

源码:

https://github.com/chsword/PinYinUtil/blob/master/PinYinUtils.cs

GITHUB:https://github.com/chsword/PinYinUtil

中文转拼音without CJK的更多相关文章

  1. Mono 3.2 测试NPinyin 中文转换拼音代码

    C#中文转换为拼音NPinyin代码  在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问题. using System; using System. ...

  2. PHP如何将中文转换为拼音

    用来得到中文的首字母: 这个是将中文转换为拼音的类:charset <?php/*** 汉字转化为拼音,拼音转化为汉字**/ class charset{private $_code=array ...

  3. php 获取中文字符拼音首字母

    //php获取中文字符拼音首字母 function getFirstCharter($str){ if(empty($str)){return '';} $fchar=ord($str{}); }); ...

  4. C# 中文转拼音类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SU { ...

  5. SQL 用中文的拼音和笔画排序

    SQL 用中文的拼音和笔画排序   城市按拼音排序: SELECT chineseName FROM [表名] order by chinesename collate Chinese_PRC_CS_ ...

  6. NPinyin 中文转换拼音代码

    Mono 3.2 测试NPinyin 中文转换拼音代码   C#中文转换为拼音NPinyin代码  在Mono 3.2下运行正常,Spacebuilder 有使用到NPinyin组件,代码兼容性没有问 ...

  7. php获取中文字符拼音首字母

    //php获取中文字符拼音首字母 function getFirstCharter($str){ if(empty($str)){ return ''; } $fchar = ord($str{0}) ...

  8. localeCompare() 方法实现中文的拼音排序

    google了很多次才发现在国外网站上有提示如何比较中文,原文地址:http://www.webdeveloper.com/forum/showthread.php?t=9365 前提:使用Unico ...

  9. Python中文转拼音代码(支持全拼和首字母缩写)

    本文的代码,从https://github.com/cleverdeng/pinyin.py升级得来,针对原文的代码,做了以下升级:     1 2 3 4 1.可以传入参数firstcode:如果为 ...

随机推荐

  1. walle2.0 nginx.conf配置文件参数

    vim /usr/local/nginx/conf #user nobody; worker_processes ; events { worker_connections ; } http{ inc ...

  2. Windows下安装Redis客户端

    Redis是有名的NoSql数据库,一般Linux都会默认支持.但在Windows环境中,可能需要手动安装设置才能有效使用.这里就简单介绍一下Windows下Redis服务的安装方法,希望能够帮到你. ...

  3. 日期时间选择器、Bootstrap日期和时间表单组件。bootstrap-datetimepicker实现年月日,时分秒的选择。

    参考链接:http://www.bootcss.com/p/bootstrap-datetimepicker/ 1.官网以及很详细的说明了如何使用,这里结合一下自己的使用来写下. 下载解压缩包以后,可 ...

  4. SoftEther

    sudo apt-get update   sudo wget http://www.softether-download.com/files/softether/v4.25-9656-rtm-201 ...

  5. selenium爬取煎蛋网

    selenium爬取煎蛋网 直接上代码 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv ...

  6. UOJ#424. 【集训队作业2018】count 多项式,FFT,矩阵

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ424.html 题解 主席太神仙了! 首先我们把题意转化成:对所有挺好序列建 笛卡尔树,有多少笛卡尔树互不 ...

  7. 了解javascript里面的 封装

    // 封装 //生成实例对象的原始模式 //假如我们把一个动物看成一个对象 var cat = { //那么它有名字和颜色两个属性 name:'', color:'' }; //接下来我们根据原形对象 ...

  8. python3_猜数字

    import random count = 0while count<3: count +=1 number = int(input("猜数字:").strip()) num ...

  9. python3中列表、元组、字典的增删改查说明详解

    python基础中的列表.元组.字典属于python中内置的序列数据结构.其中序列可以进行的操作包括索引.截取(切片).加.乘.成员检查等. 1.列表 列表(list)是最常用的python数据类型之 ...

  10. linux系统,关于Python多版本共存

    http://www.cnblogs.com/Yiutto/p/5962906.html 给个地址直接看八~