c# 中文转拼音without CJK

作者:重典 时间:2023-06-08 23:59:31 

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

来源:http://www.cnblogs.com/chsword/p/xamarin_chinese_to_pinyin.html

标签:中文,拼音
0
投稿

猜你喜欢

  • java-spark中各种常用算子的写法示例

    2023-04-28 23:21:01
  • Java创建线程池为什么一定要用ThreadPoolExecutor

    2023-04-22 06:03:31
  • Android TextView中文字通过SpannableString设置属性用法示例

    2023-07-26 07:11:51
  • TextView实现跑马灯效果 就这么简单!

    2023-06-25 18:42:24
  • C#算法之整数反转

    2021-09-24 18:36:49
  • idea 打包maven项目忽略test文件的操作

    2021-10-16 07:21:14
  • SpringBoot基于Actuator远程关闭服务

    2022-06-24 21:36:37
  • 详解Java目录操作与文件操作教程

    2023-12-05 13:45:48
  • C#探秘系列(三)——StackTrace,Trim

    2022-04-27 21:24:03
  • Java读取.properties配置文件方法示例

    2023-08-24 16:32:56
  • Android Mms之:对话与联系人关联的总结详解

    2023-12-06 13:12:57
  • 分析设计模式之模板方法Java实现

    2022-01-20 21:02:22
  • Java常用线程池原理及使用方法解析

    2022-02-22 17:00:23
  • SpringCloud Eureka服务治理之服务注册服务发现

    2021-12-27 15:07:16
  • 详解MyBatis直接执行SQL查询及数据批量插入

    2021-12-02 17:52:08
  • 详解Java Selenium中的鼠标控制操作

    2023-11-23 14:43:17
  • .net文件上传时实现通过文件头确认文件类型的方法

    2021-08-01 09:05:32
  • 基于Spring AOP proxyTargetClass的行为表现总结

    2022-05-23 18:33:43
  • SpringBoot中@Import注解如何正确使用

    2023-07-28 12:36:16
  • 使用 Java8 实现观察者模式的方法(下)

    2021-08-03 04:21:37
  • asp之家 软件编程 m.aspxhome.com