C# 获取汉字的拼音首字母

一种是把所有中文字符集合起来组成一个对照表;另一种是依照汉字在Unicode编码表中的排序来确定拼音的首字母。碰到多音字时就以常用的为准(第一种方法中可以自行更改,方法为手动把该汉字移动到对应的拼音首字母队列,我们这里介绍第二种。

获取汉字拼音的首字母是一个在做项目的过程中经常需要用到的功能,今天我们主要来探讨下C# 获取汉字的拼音首字母

    static void Main(string[] args)
{ Console.WriteLine(GetSpellCode("asdf牛逼你水电费")) ;
Console.ReadKey();
} /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnStr">汉字字符串</param> /// <returns>相对应的汉语拼音首字母串</returns> public static string GetSpellCode(string CnStr)
{ string strTemp = ""; int iLen = CnStr.Length; int i = 0; for (i = 0; i <= iLen - 1; i++)
{ strTemp += GetCharSpellCode(CnStr.Substring(i, 1)); } return strTemp; } /// <summary>
/// 得到一个汉字的拼音第一个字母,如果是一个英文字母则直接返回大写字母
/// </summary>
/// <param name="CnChar">单个汉字</param>
/// <returns>单个大写字母</returns> private static string GetCharSpellCode(string CnChar)
{ long iCnChar; byte[] ZW = System.Text.Encoding.Default.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 ("?"); }

C# 获取汉字的拼音首字母和全拼(含源码)的更多相关文章

  1. (转载)delphi中获取汉字的拼音首字母

    delphi中获取汉字的拼音首字母1.py: array[216..247] of string = ({216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + ' ...

  2. sql获取汉字的拼音首字母的函数

    ql获取汉字的拼音首字母   if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and ...

  3. 获取汉字的拼音首字母--pinyin

    var pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, options = { checkPo ...

  4. sql获取汉字的拼音首字母

    if exists (select * from sysobjects where id = object_id(N'[fn_ChineseToSpell]') and xtype in (N'FN' ...

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

    /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnS ...

  6. SqlServer 笔记二 获取汉字的拼音首字母

    一.该函数传入字符串,返回数据为:如果为汉字字符,返回该字符的首字母,如果为非汉字字符,则返回本身. 二.用到的知识点:汉字对应的UNICODE值,汉字的排序规则. 三.数据库函数: )) ) AS ...

  7. SqlServer 获取汉字的拼音首字母

    一.该函数传入字符串,返回数据为:如果为汉字字符,返回该字符的首字母,如果为非汉字字符,则返回本身.二.用到的知识点:汉字对应的UNICODE值,汉字的排序规则.三.数据库函数: CREATE FUN ...

  8. C#获取包括一二级汉字的拼音 首字母

    C#获取包括一二级汉字的拼音 首字母 声母 汉字拼音转换 using System; using System.Collections.Generic; using System.Linq; usin ...

  9. js汉字转拼音首字母

    js汉字转拼音首字母 2018-04-09 阅读 1018 收藏 1 原链:segmentfault.com 分享到:   前端必备图书<JavaScript设计模式与开发实践> > ...

随机推荐

  1. MGTemplateEngine 模版发动机简单使用

    https://github.com/nxtbgthng/MGTemplateEngine MGTemplateEngine 模版引擎 MGTemplateEngine比較象 PHP 中的 Smart ...

  2. C#.NET开源项目、机器学习、Power BI (转载)

    .NET技术, 开源项目, 数据挖掘, 机器学习, 微软Power BI, 足球赛事分析, Matlab与C#编程 博客园 管理 本站首页 头条推荐 Power BI .NET开源 机器学习 博客美化 ...

  3. Java Web Start

    1. JNLP 2. Security issue: https://java.com/en/download/help/win_controlpanel.xml Windows 7, Vista C ...

  4. ASP.NET页面之间传值的几种方式

    1.  QueryString 当页面上的form以get方式向页面发送请求数据时,web server将请求数据放入一名为QEURY_STRING的环境变量中,QeueryString方法从这个变量 ...

  5. MongoDB的一些操作技巧

    去年三月底入职上海的一家互联网公司,由于项目使用的是MongoDB数据库所以有机会接触了MongoDB.在项目的开发过程中使用系统原有的一些方法查询MongoDB感觉很费力,用起来也不爽,所以私下里就 ...

  6. 使用无缓冲IO函数读写文件

    前言 本文介绍使用无缓冲IO函数进行文件读写. 所谓的无缓冲是指该IO函数通过调用系统调用实现,其实系统调用内部的读写实现也是使用了缓冲技术的. 读写步骤 1. 打开文件 open 函数 2. 读写文 ...

  7. Drawing Images and Text

    using System;using UIKit;using Foundation;using CoreGraphics;namespace GraphicsAnimation{ public cla ...

  8. zoj 2711 - Regular Words

    题目:求由A.B.C构成的有序传中长度为n.且每一个B前面的A的个数不少于当前B,每一个C前面的B的个数不少于当前C的个数. 分析:dp,求排列组合数. 考虑二维的状况: 假设 A>=B 则在 ...

  9. 红黑树深入剖析及Java实现(转自知乎美团点评技术团队)

    作者:美团点评技术团队 链接:https://zhuanlan.zhihu.com/p/24367771 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 红黑树是平衡 ...

  10. 操作系统 资源管理 zookeeper yarn 进程管理 分布式 yarn诞生背景

    zookeeper 信息保管员 YARN 简介 https://www.ibm.com/developerworks/cn/data/library/bd-yarn-intro/index.html