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. Unity简单塔防游戏的开发——敌人移动路径的创建及移动

    软件工程综合实践专题第一次作业 Unity呢是目前一款比较火热的三维.二维动画以及游戏的开发引擎,我也由于一些原因开始接触并喜爱上了这款开发引擎,下面呢是我在学习该引擎开发小项目时编写的一些代码的脚本 ...

  2. C语言中return 0和return 1和return -1

    转载声明:本文系转载文章 原文作者:十一月zz 原文地址:https://blog.csdn.net/baidu_35679960/article/details/77542787 1.返回值int  ...

  3. 强大而灵活的的Html解析器——Html Agility Pack

    一.概述 Html Agility Pack 简称HAP,是一个强大而灵活的解析Html DOM的.Net类库. 二.官方链接 官网:http://html-agility-pack.net/ NuG ...

  4. C# Levenshtein计算字符串的相似度

    static void Main(string[] args) { Levenshtein(@"今天天气不错", @"今天的天气不错啊"); Console.R ...

  5. Aspnet Core 对 Resetful API版本的支持

    在实际项目过程中API往往会收到迭代的影响,同时具备多个版本,因此resetful接口的版本话是非常重要的. 其实官方就提供了很好的支持,微软爸爸在nuget提供了Microsoft.AspNetCo ...

  6. JMeter通过beanShell脚本生成随机手机号

    package xnzx; /** * @author xn088587 * */ public class getTel{ public static int getNum(int start,in ...

  7. P1522 牛的旅行 Cow Tours floyed

    题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...

  8. Unity Shader Learning

    Toon 表面没有均匀的阴影. 为了达到这个效果,我们需要一个斜坡图. 其目的是将朗伯光强度NdotL重新映射到另一个值. 使用没有渐变的渐变映射,我们可以强制照明逐步渲染.下图显示了如何使用斜坡图来 ...

  9. C++入门笔记(一)零碎基础知识

    零碎基础知识 一.创建和运行程序 1.使用文本编辑器编写程序,保存为文件,该文件就叫源代码. 2.编译源代码:运行一个程序,将源代码翻译为主机使用的内部语言----机器语言.包含了 编译后程序的文件就 ...

  10. Kafka 安装配置

    1. 下载安装kafka 下载地址:http://apache.fayea.com/kafka/ 解压安装包 tar zxvf kafka_版本号.tgz 2. 配置 修改kafka的config/s ...