今天突然要用到提取汉字拼音首字母的功能,去网上找了找,发现没有几个好用的,决定自己写一个,效果还不错,发出来大家一起研究下,分享给大家!直接入主题:

1.首先对编码进行定义

#region 编码定义

private static int[] pyvalue = new int[] 
        { 
        -20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032, 
        -20026, 
        -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, 
        -19741, -19739, -19728, 
        -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, 
        -19275, -19270, -19263, 
        -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, 
        -19006, -19003, -18996, 
        -18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, 
        -18697, -18696, -18526, 
        -18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, 
        -18201, -18184, -18183, 
        -18181, -18012, -17997, -17988, -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, 
        -17752, -17733, -17730, 
        -17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, 
        -17427, -17417, -17202, 
        -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706, -16689, -16664, -16657, -16647, -16474, 
        -16470, -16465, -16459, 
        -16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412, -16407, -16403, -16401, -16393, -16220, 
        -16216, -16212, -16205, 
        -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959, -15958, -15944, -15933, -15920, -15915, 
        -15903, -15889, -15878, 
        -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631, -15625, -15454, -15448, -15436, 
        -15435, -15419, -15416, 
        -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180, -15165, -15158, -15153, 
        -15150, -15149, -15144, 
        -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, -14937, -14933, 
        -14930, -14929, -14928, 
        -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, -14678, 
        -14674, -14670, -14668, 
        -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, 
        -14345, -14170, -14159, 
        -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, 
        -14094, -14092, -14090, 
        -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, 
        -13847, -13831, -13658, 
        -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, 
        -13343, -13340, -13329, 
        -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, 
        -12888, -12875, -12871, 
        -12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, 
        -12556, -12359, -12346, 
        -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, 
        -11798, -11781, -11604, 
        -11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, 
        -11041, -11038, -11024, 
        -11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, 
        -10533, -10519, -10331, 
        -10329, -10328, -10322, -10315, -10309, -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, 
        -10254 
        };

private static string[] pystr = new string[] 
        { 
        "a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", 
        "biao", 
        "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan" 
        , "chang", "chao", "che", "chen", 
        "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong" 
        , "cou", "cu", "cuan", "cui", 
        "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", 
        "diu", "dong", "dou", "du", "duan", 
        "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga" 
        , "gai", "gan", "gang", "gao", 
        "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", 
        "ha", "hai", "han", "hang", 
        "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", 
        "huo", "ji", "jia", "jian", 
        "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", 
        "kang", "kao", "ke", "ken", 
        "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", 
        "lang", "lao", "le", "lei", 
        "leng", "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", 
        "luan", "lue", "lun", "luo", 
        "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", 
        "ming", "miu", "mo", "mou", "mu", 
        "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", 
        "nin", "ning", "niu", "nong", 
        "nu", "nv", "nuan", "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", 
        "pi", "pian", "piao", "pie", 
        "pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", 
        "qu", "quan", "que", "qun", 
        "ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", 
        "sai", "san", "sang", 
        "sao", "se", "sen", "seng", "sha", "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", 
        "shou", "shu", "shua", 
        "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", 
        "suo", "ta", "tai", 
        "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie", "ting", "tong", "tou", "tu", "tuan", 
        "tui", "tun", "tuo", 
        "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", "xia", "xian", "xiang", "xiao", 
        "xie", "xin", "xing", 
        "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi", "yin", "ying", "yo", 
        "yong", "you", 
        "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", "zhai" 
        , "zhan", "zhang", 
        "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", 
        "zhui", "zhun", "zhuo", 
       
   #endregion

2.然后就是拼音的处理方法了

#region 拼音处理

/// <summary> 
        /// 将一串中文转化为拼音
        /// 如果给定的字符为非中文汉字将不执行转化,直接返回原字符;
        /// </summary> 
        /// <param name="chsstr">指定汉字</param> 
        /// <returns>拼音码</returns> 
        public string ChsString2Spell(string chsstr)
        {
            string strRet = string.Empty;

char[] ArrChar = chsstr.ToCharArray();

foreach (char c in ArrChar)
            {
                strRet += SingleChs2Spell(c.ToString());
            }

return strRet;
        }
        /// <summary> 
        /// 将一串中文转化为拼音
        /// </summary> 
        /// <param name="chsstr">指定汉字</param> 
        /// <returns>拼音首字母</returns> 
        public string GetHeadOfChs(string chsstr)
        {
            string strRet = string.Empty;

char[] ArrChar = chsstr.ToCharArray();

foreach (char c in ArrChar)
            {
                strRet += GetHeadOfSingleChs(c.ToString());
            }

return strRet;
        }

/// <summary> 
        /// 单个汉字转化为拼音
        /// </summary> 
        /// <param name="SingleChs">单个汉字</param> 
        /// <returns>拼音</returns> 
        public string SingleChs2Spell(string SingleChs)
        {
            byte[] array;
            int iAsc;
            string strRtn = string.Empty;

array = Encoding.Default.GetBytes(SingleChs);

try
            {
                iAsc = (short)(array[0]) * 256 + (short)(array[1]) - 65536;
            }
            catch
            {
                iAsc = 1;
            }

if (iAsc > 0 && iAsc < 160)
                return SingleChs;

for (int i = (pyvalue.Length - 1); i >= 0; i--)
            {
                if (pyvalue[i] <= iAsc)
                {
                    strRtn = pystr[i];
                    break;
                }
            }

//将首字母转为大写
            if (strRtn.Length > 1)
            {
                strRtn = strRtn.Substring(0, 1).ToUpper() + strRtn.Substring(1);
            }

return strRtn;
        }

/// <summary> 
        /// 得到单个汉字拼音的首字母
        /// </summary> 
        /// <returns> </returns> 
        public string GetHeadOfSingleChs(string SingleChs)
        {
            return SingleChs2Spell(SingleChs).Substring(0, 1);
        }
    #endregion

调用方法相信大家都会了,就不多说了!

转载:http://www.cnblogs.com/yazdao/archive/2011/06/03/2072124.html

(转载)C#提取汉字拼音首字母的方法的更多相关文章

  1. C# 汉字转拼音 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母

    代码参考该文http://www.cnblogs.com/yazdao/archive/2011/06/04/2072488.html VS2015版本 1.使用Nuget 安装 "Simp ...

  2. C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母

    首先下载Visual Studio International Pack 1.0,官方下载地址:http://www.microsoft.com/downloads/zh-cn/details.asp ...

  3. php获取汉字拼音首字母的方法

    现实中我们经常看到这样的说明,排名不分先后,按姓名首字母进行排序.这是中国人大多数使用的排序方法.那么在php程序中该如何操作呢? 下面就分享一下在php程序中获取汉字拼音的首字母的方法,在网上搜到的 ...

  4. excel提取汉字拼音首字母

    本文转载:http://jingyan.baidu.com/article/63acb44adca44461fcc17e85.html 利用Excel表格中的宏,轻松提取首字母 方法/步骤 1.启动E ...

  5. C# 获取汉字拼音首字母

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨C#获取汉字拼音首字母的方法: 代码类东西, ...

  6. C# 获取汉字拼音首字母/全拼

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨C#获取汉字拼音首字母的方法: 代码类东西, ...

  7. 终结者:借助pinyin4j相关jar包提取汉字的首字母

    import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...

  8. AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)

    AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实 ...

  9. MySQL通过函数获取字符串汉字拼音首字母大写字符串

    DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARC ...

随机推荐

  1. 在Windows 10中更改网络连接优先级

    查看接口列表 (也可使用 如下) 选择网络连接,然后单击右侧的箭头以更改网络连接优先级.  可以参考之前的部分 链接在此 更改单个wi-fi连接顺序可以使用如下

  2. 2019.3.25 IDEA控制台乱码解决 &&idea关闭代码自动提示

    设置Tomcat里面的conf文件夹下的properties结尾的文件

  3. cocos2d-x开发: 完善接口范例分离模块

    在上一篇文章中,我阐述了一下为什么要做分离这种工作,这篇文章没什么重点,只是将上一次没有完善的工作做完.原本我想做到像Lua-tests那样子,能够一次完全显示所有的tests,然后选择要执行的tes ...

  4. EF中的Guid主键

    除了自增长ID(int),我们还能把主键设置为GUID类型的. 创建我们的数据表 CREATE TABLE dbo.JoinA( AGUID UNIQUEIDENTIFIER PRIMARY KEY ...

  5. What to do next to activate this settings for already existing users

    Link: http://sharepoint.stackexchange.com/questions/89284/sharepoint-2013-mysite-increase-quota Cent ...

  6. iOS开发UI篇 -- UISearchBar 属性、方法详解及应用(自定义搜索框样式)

    很多APP都会涉及到搜索框,苹果也为我们提供了默认的搜索框UISearchBar.但实际项目中我们通常需要更改系统默认搜索框的样式.为了实现这一目标,我们需要先搞懂 UISearchBar 的属性及方 ...

  7. Web | Webpack快速上手

    概述 Webpack 是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源. 一般我们在开发中都是使用终端去进行安装,使用npm,关于npm ...

  8. K3CLOUD开发-动态表单树形单据体实现银行交易对账

    背景:系统手机开单生成销售单据,通过银行pos机收款,系统收款流水与银行流水可能存在差异,所以通过获取银行接口,获取消费信息自动插入到生产系统数据库,开发对账报表,实现差异汇总! 展示效果如下: 开发 ...

  9. Web的基本工作原理、HTTP协议和URL说明

    Web工作原理 客户端和Web服务器通过HTTP协议进行通信.Web服务器有是也叫HTTP服务器或Web容器.HTTP协议采用的是请求/响应模式.即客户端发起HTTP请求,web服务器接收并解析处理H ...

  10. ajax与jsonp定义及使用方法

    ajax 定义 ajax技术的目的是让javascript发送http请求,与后台通信,获取数据和信息. ajax通信的过程不会影响后续javascript的执行,从而实现异步. 同步和异步 现实生活 ...