C# 获取汉字拼音首字母

最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。

十年河东十年河西,莫欺少年穷

学无止境,精益求精  

本节探讨C#获取汉字拼音首字母的方法:

代码类东西,直接上代码:

        /// <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));
                break;
            }

            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 CommonMethod.CutString(CnChar.ToUpper(),1);

            }
            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 ("?");

        }

截取字符串的方法:

#region 截取字符长度 static string CutString(string str, int len)
        /// <summary>
        /// 截取字符长度
        /// </summary>
        /// <param name="str">被截取的字符串</param>
        /// <param name="len">所截取的长度</param>
        /// <returns>子字符串</returns>
        public static string CutString(string str, int len)
        {
            if (str == null || str.Length == 0 || len <= 0)
            {
                return string.Empty;
            }

            int l = str.Length;

            #region 计算长度
            int clen = 0;
            while (clen < len && clen < l)
            {
                //每遇到一个中文,则将目标长度减一。
                if ((int)str[clen] > 128) { len--; }
                clen++;
            }
            #endregion

            if (clen < l)
            {
                return str.Substring(0, clen) + "...";
            }
            else
            {
                return str;
            }
        }

        /// <summary>
        /// //截取字符串中文 字母
        /// </summary>
        /// <param name="content">源字符串</param>
        /// <param name="length">截取长度!</param>
        /// <returns></returns>
        public static string SubTrueString(object content, int length)
        {
            string strContent = NoHTML(content.ToString());

            bool isConvert = false;
            int splitLength = 0;
            int currLength = 0;
            int code = 0;
            int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
            int chend = Convert.ToInt32("9fff", 16);
            for (int i = 0; i < strContent.Length; i++)
            {
                code = Char.ConvertToUtf32(strContent, i);
                if (code >= chfrom && code <= chend)
                {
                    currLength += 2; //中文
                }
                else
                {
                    currLength += 1;//非中文
                }
                splitLength = i + 1;
                if (currLength >= length)
                {
                    isConvert = true;
                    break;
                }
            }
            if (isConvert)
            {
                return strContent.Substring(0, splitLength);
            }
            else
            {
                return strContent;
            }
        }

        public static int GetStringLenth(object content)
        {
            string strContent = NoHTML(content.ToString());
            int currLength = 0;
            int code = 0;
            int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
            int chend = Convert.ToInt32("9fff", 16);
            for (int i = 0; i < strContent.Length; i++)
            {
                code = Char.ConvertToUtf32(strContent, i);
                if (code >= chfrom && code <= chend)
                {
                    currLength += 2; //中文
                }
                else
                {
                    currLength += 1;//非中文
                }

            }
            return currLength;
        }
        #endregion

以上便是完整代码,谢谢!

在此,顺便说下数据库按照汉字首字母进行排序的方法:

oracle :

在oracle9i中新增了按照拼音、部首、笔画排序功能。设置NLS_SORT值
SCHINESE_RADICAL_M 按照部首(第一顺序)、笔划(第二顺序)排序
SCHINESE_STROKE_M 按照笔划(第一顺序)、部首(第二顺序)排序
SCHINESE_PINYIN_M 按照拼音排序,系统的默认排序方式为拼音排序

举例如下:
表名为 dept ,其中name字段是中文,下面分别实现按照单位名称的笔划、部首和拼音排序。
//按照笔划排序
select * from dept order by nlssort(name,‘NLS_SORT=SCHINESE_STROKE_M‘);
 //按照部首排序
 select * from dept order by nlssort(name,‘NLS_SORT=SCHINESE_RADICAL_M‘);
//按照拼音排序,此为系统的默认排序方式
select * from dept order by nlssort(name,‘NLS_SORT=SCHINESE_PINYIN_M‘);

sqlserver

select * from table order by name collate Chinese_PRC_CS_AS_KS_WS 

执行结果:select * from Table where cateFid=0 order by cateName collate Chinese_PRC_CS_AS_KS_WS 

@陈卧龙的博客

时间: 2024-08-02 10:57:55

C# 获取汉字拼音首字母的相关文章

ios/android获取汉字拼音首字母

android/ios开发如何获取汉字拼音的首字母? 提供的一个比较好的方案,是使用 c语言编写的一个算法,可以 在ios和 android.下面提供一个可以使用的代码,并且附上使用方法: /* * pinyin.c * Chinese Pinyin First Letter * * Created by George on 4/21/10. * Copyright 2010 RED/SAFI. All rights reserved. * */ #define HANZI_START 1996

java获取汉字拼音首字母 --转载

在项目中要更能根据某些查询条件(比如姓名)的首字母作为条件进行查询,比如查一个叫"李晓明"的人,可以输入'lxm'.写了一个工具类如下: import java.io.UnsupportedEncodingException; /** * 取得给定汉字串的首字母串,即声母串 * Title: ChineseCharToEn * @date 2004-02-19 注:只支持GB2312字符集中的汉字 */ public final class ChineseCharToEn { priv

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

现实中我们经常看到这样的说明,排名不分先后,按姓名首字母进行排序.这是中国人大多数使用的排序方法.那么在php程序中该如何操作呢? 下面就分享一下在php程序中获取汉字拼音的首字母的方法,在网上搜到的大多数是有问题的,这个可是经过小编实践应用过的,真的可以使用的哦. //php获取中文字符拼音首字母 function getFirstCharter($str){  if(empty($str)){return '';}  $fchar=ord($str{0});  if($fchar>=ord(

如何获取汉字拼音首字母?一般用于通讯录

- (NSString *)firstCharactor:(NSString *)aString { //转成了可变字符串 NSMutableString *str = [NSMutableString stringWithString:aString]; //先转换为带声调的拼音 CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO); //再转换为不带声调的拼音 CFStringT

工作问题:如何获取汉字拼音首字母?一般用于通讯录建设

/* 程序员的目标是 征服星辰的大海~ */ - (NSString *)firstCharactor:(NSString *)aString { //转成了可变字符串 NSMutableString *str = [NSMutableString stringWithString:aString]; //先转换为带声调的拼音 CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);

C/C++ 获取汉字拼音首字母

1 #include <stdint.h> 2 #include <stdio.h> 3 #include <ctype.h> 4 #include <string.h> 5 6 bool between(uint32_t start,uint32_t end,uint32_t aim); 7 char get_first_letter(wchar_t wchar); 8 void get_first_letters(const char *szChines

【JAVA】获取汉字拼音首字母

最近在做通讯录的时候,需要把姓转换为拼音字母 1-9 a-z A-Z 转换为#:借张微信的图大家感受下 网上的代码很多,不外乎两种 1:pinyin4java包太大 2:大部分不支持生僻字,比如“栾.鑫” 认不出 本方案解决了这个问题,就很简单一个helper类,注意,只是拼音首字母哦!且编码格式为GBK! 代码很简单,就是在正常GBK检索不到的生僻字上,再加入一个字典,如果GBK检索不到,则在字典里找. package zhexian.app.smartcall.Utils; import j

php 获取汉字拼音首字母的函数

<?php header("content-type:text/html;charset=utf-8"); function getfirstchar($s0){ $firstchar_ord=ord(strtoupper($s0{0})); if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0};

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

DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARCHAR(21845)) RETURNS VARCHAR(21845) CHARSET utf8 BEGIN #截取字符串,每次做截取后的字符串存放在该变量中,初始为函数参数in_string值 DECLARE tmp_str VARCHAR(21845) CHARSET gbk DEFAULT ''