博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Phone实用开发技巧(34):获取汉字拼音的首字母
阅读量:6870 次
发布时间:2019-06-26

本文共 3425 字,大约阅读时间需要 11 分钟。

在之前的文章中曾经介绍过如何在,当时的方法是将所有的汉字、拼音放到一个集合中,然后匹配查找的,这样就会导致一个问题,就是没有在集合中的汉字是找不到拼音的,也会有一些异常情况的发生。

今天介绍一种新的方法,使用codeplex上开源的项目.

1.创建一个项目,命名为mangoGB2312

2.将GB2312 for Silverlight项目中的两个文件都拷贝至项目目录GB2312下,并且修改GB2312Encoding.cs的命名空间为mangoGB2312.GB2312,如下图所示

3.将gb2312.bin的生成方式修改为嵌入式资源,如下图

4.编写提取汉字首字母的函数,我已经封装到一个帮助类里面了,代码如下:

public class FirstLetterHelper{    ///      /// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母     ///      /// 单个汉字     /// 
单个大写字母
public static string GetCharSpellCode(string CnChar) { long iCnChar; GB2312Encoding encoder = new GB2312Encoding(); byte[] ZW = encoder.GetBytes(CnChar); //如果是字母,则直接返回 if (ZW.Length == 1) { return CnChar.ToUpper(); } else { // get the array of byte from the single char int i1 = (short)(ZW[0]); int i2 = (short)(ZW[1]); iCnChar = i1 * 256 + i2; } // iCnChar match the constant if ((iCnChar >= 45217) && (iCnChar <= 45252)) { return "A"; } else if ((iCnChar >= 45253) && (iCnChar <= 45760)) { return "B"; } else if ((iCnChar >= 45761) && (iCnChar <= 46317)) { return "C"; } else if ((iCnChar >= 46318) && (iCnChar <= 46825)) { return "D"; } else if ((iCnChar >= 46826) && (iCnChar <= 47009)) { return "E"; } else if ((iCnChar >= 47010) && (iCnChar <= 47296)) { return "F"; } else if ((iCnChar >= 47297) && (iCnChar <= 47613)) { return "G"; } else if ((iCnChar >= 47614) && (iCnChar <= 48118)) { return "H"; } else if ((iCnChar >= 48119) && (iCnChar <= 49061)) { return "J"; } else if ((iCnChar >= 49062) && (iCnChar <= 49323)) { return "K"; } else if ((iCnChar >= 49324) && (iCnChar <= 49895)) { return "L"; } else if ((iCnChar >= 49896) && (iCnChar <= 50370)) { return "M"; } else if ((iCnChar >= 50371) && (iCnChar <= 50613)) { return "N"; } else if ((iCnChar >= 50614) && (iCnChar <= 50621)) { return "O"; } else if ((iCnChar >= 50622) && (iCnChar <= 50905)) { return "P"; } else if ((iCnChar >= 50906) && (iCnChar <= 51386)) { return "Q"; } else if ((iCnChar >= 51387) && (iCnChar <= 51445)) { return "R"; } else if ((iCnChar >= 51446) && (iCnChar <= 52217)) { return "S"; } else if ((iCnChar >= 52218) && (iCnChar <= 52697)) { return "T"; } else if ((iCnChar >= 52698) && (iCnChar <= 52979)) { return "W"; } else if ((iCnChar >= 52980) && (iCnChar <= 53640)) { return "X"; } else if ((iCnChar >= 53689) && (iCnChar <= 54480)) { return "Y"; } else if ((iCnChar >= 54481) && (iCnChar <= 55289)) { return "Z"; } else return ("#"); }}

5.测试程序,将模拟器中的键盘勾选上中文输入法,如下图

6.输入一些文字后,点击GET就可以得到汉字的首字母了

你可以在这里找到本篇文章的

转载于:https://www.cnblogs.com/alexis/archive/2012/02/05/Get_FirstChar_of_ChineseWord.html

你可能感兴趣的文章
[转] VB6.0 Dictionary 排序,生成Sign
查看>>
selenium IDE安装与使用
查看>>
GNU C - Using GNU GCC __attribute__ mechanism 02 Variable Attribute && Type Attribute
查看>>
java 反射机制的概念
查看>>
回调函数和钩子函数的区别
查看>>
jsp之tomcat安装
查看>>
Vue导航守卫
查看>>
Xsocket学习
查看>>
lnmp下django学习
查看>>
2016年全国高中数学联合竞赛试题及详细解答
查看>>
Thymeleaf利用layout.html文件生成页面布局框架
查看>>
PlatformTransactionManager
查看>>
读懂系统负载(Load Avg)的含义 | Devops
查看>>
Linux命令对应的全称解释(转)
查看>>
验证码 和 验证控件
查看>>
struts2 ActionSupport关联源码
查看>>
【转】源码安装Mysql,补装innodb引擎方法
查看>>
[原]虚拟机(ubuntu)无法ping通主机
查看>>
android Build系统
查看>>
HTML5本地存储——IndexedDB(一:基本使用)
查看>>