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. mysql优化之索引建立的规则

    索引经常使用的数据结构为B+树.结构例如以下 如上图,是一颗b+树,关于b+树的定义能够參见B+树,这里仅仅说一些重点.浅蓝色的块我们称之为一个磁盘块,能够看到每一个磁盘块包括几个数据项(深蓝色所看到 ...

  2. POSIX标准中的 “ 限制 ”

    前言 在POSIX标准中,定义了许多限制.这些限制大约分为五类,不同类型的限制获取的方式不一样. 限制值分类 1. 不变的最小值 这类型的限制值是静态的,固定的. 2. 不变值 同上 3. 运行时可以 ...

  3. Android获取手机方向

    假设用户开启了设置里的屏幕旋转,Android中处理横竖屏切换,通常的做法是在AndroidManifest.xml中定义android:configChanges="orientation ...

  4. 3932: [CQOI2015]任务查询系统

    3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2559  Solved: 819[Submit][Sta ...

  5. 【BZOJ4407】于神之怒加强版 莫比乌斯反演

    [BZOJ4407]于神之怒加强版 Description 给下N,M,K.求 Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行, ...

  6. mysql分页查询-limit

    分页查询的sql: select * from table limit 4,10; 4表示查询的索引,索引是从0开始,4表示从第五条数据开始查询,10表示要查询多少条数据,10表示查询十条数据 如果从 ...

  7. leetcode 748. Shortest Completing Word

    Find the minimum length word from a given dictionary words, which has all the letters from the strin ...

  8. 《编程之美》之如何控制CPU的暂用率固定在50%

    <编程之美>第一章 让CPU暂用率听你指挥的粗糙实现,如何控制CPU的暂用率固定在50% #include <stdio.h> #include <Windows.h&g ...

  9. HDU4965 Fast Matrix Calculation —— 矩阵乘法、快速幂

    题目链接:https://vjudge.net/problem/HDU-4965 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Othe ...

  10. LightOJ1213 Fantasy of a Summation —— 快速幂

    题目链接:https://vjudge.net/problem/LightOJ-1213 1213 - Fantasy of a Summation    PDF (English) Statisti ...