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. Tensorflow笔记一

    Tensorlfow中的计算是通过一个有向图directed graph或则计算图computation graph来实现的. 将每一个运算操作operation作为一个节点node,节点之间通过边e ...

  2. 使用 eclipse 的常用操作

    1.创建项目 https://blog.csdn.net/tsundere_ning/article/details/79587060 2. 常用代码块创建编辑 使得eclipse 相应, 点击右上角 ...

  3. Vue:window.onresize

    1. 添加属性screenHeight 和 timer. screenHeight: window.innerHeight timer: '' //  window.onresize函数频繁调用时,页 ...

  4. C++中的继承(3)作用域与重定义,赋值兼容规则

    1.作用域与重定义(同名隐藏) 一样的,先上代码 1 class A 2 { 3 public: 4 int a_data; 5 void a() 6 { 7 cout << " ...

  5. docker删除已经停止的容器

    前言:docker容器已经停止运行的容器,怎么清理 1.如图:   docker   ps  -a  :显示所有运行过的docker容器 status    :  docker容器的状态 docker ...

  6. 生成免费SSL通配证书

    通过Let's Encrypt 生成免费SSL证书 有效期是3个月 1.下载工具certbot-auto wget https://dl.eff.org/certbot-auto chmod +x c ...

  7. java的this关键字理解

    1.java提供了一个this关键字,this关键字总是指向调用该方法的对象.根据this出现位置的不同,this作为对象的默认引用有两种情形.a).构造器中引用该构造器正在初始化的对象.(this总 ...

  8. linux su失败:无法设置用户ID:资源暂时不可用

    环境 linux RHEP 7.+ su - user 提示 :无法设置用户ID,资源暂时不可用 检查 cat /etc/security/limits.d/90-nproc.conf * soft ...

  9. Anaconda python环境管理

    1.查看conda的版本: conda --version 2. 查看当前系统安装已的python环境: conda info --envs 3. 添加python环境: conda create - ...

  10. 【spring】-- jsr303参数校验器

    一.为什么要进行参数校验? 当我们在服务端控制器接受前台数据时,肯定首先要对数据进行参数验证,判断参数是否为空?是否为电话号码?是否为邮箱格式?等等. 这里有个问题要注意: 前端代码一般上会对这些数据 ...