博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
传入含中文的字符串 返回中文首字母
阅读量:6966 次
发布时间:2019-06-27

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

hot3.png

输入含中文的字符串  返回中文的首字母private string returnzimu(string zimustr)

        {

            string blont = zimustr;

            if (Regex.IsMatch(zimustr, @"[\u4e00-\u9fa5]"))

            {

 

                for (int q = 0; q < blont.Length; q++)

                {

                    if (Regex.IsMatch(blont[q].ToString(), @"[\u4e00-\u9fa5]"))

                    {

                        blont = blont.Replace(blont[q].ToString(), getSpell(blont[q].ToString()));

 

                    }

                }

            }

            blont = blont.Replace("(""(");

            blont = blont.Replace(")"")");

            return blont;

        }

 /// <summary>  

        /// 获取单个中文的首字母  

        /// </summary>  

        /// <param name="cnChar"></param>  

        /// <returns></returns>  

        private static string getSpell(string cnChar)

        {

            byte[] arrCN = Encoding.Default.GetBytes(cnChar);

            if (arrCN.Length > 1)

            {

                int area = (short)arrCN[0];

                int pos = (short)arrCN[1];

                int code = (area << 8) + pos;

                int[] areacode = { 45217, 45253, 45761, 46318, 46826, 47010, 47297, 47614, 48119, 48119, 49062, 49324, 49896, 50371, 50614, 50622, 50906, 51387, 51446, 52218, 52698, 52698, 52698, 52980, 53689, 54481 };

 

                for (int i = 0; i < 26; i++)

                {

                    int max = 55290;

                    if (i != 25)

                    {

                        max = areacode[i + 1];

                    }

                    if (areacode[i] <= code && code < max)

                    {

                        return Encoding.Default.GetString(new byte[] { (byte)(97 + i) });

                    }

                }

                return "*";

            }

            else return cnChar;

        }  

 

 

转载于:https://my.oschina.net/u/2494395/blog/521506

你可能感兴趣的文章
Reactor-Guice 0.0.7 版本发布 ,BUG 修复,自定义模板支持
查看>>
超详细!上线一个机器学习项目你需要哪些准备?
查看>>
IIS如何设置可以让.aspx后缀的文件直接下载
查看>>
为什么区块链对中小企业至关重要?
查看>>
笨办法学 Linux 中文版 翻译完成
查看>>
js 写入图片Exif信息piexifjs
查看>>
IBM携手三菱东京日联银行 将区块链用于合同管理
查看>>
Mac 10.12安装WebStorm
查看>>
Spring Cloud启动应用时指定IP或忽略某张网卡配置
查看>>
Jenkins配置MSBuild实现自动部署2(项目实践)
查看>>
kafka好文章
查看>>
IBM发布超强量子计算机,可处理50个量子位
查看>>
如何使用Bro IDS和Intel Critical Stack分析网络活动
查看>>
Memcached的Web管理工具MemAdmin(待实践)
查看>>
嵌入式学习难点 嵌入式软件学习
查看>>
11204 ASM 在线存储迁移。
查看>>
eclipse不会自动编译的问题解决
查看>>
linux netstat命令
查看>>
淘宝卖家遭恶退诈骗 阿里一年来协助警方抓获103人
查看>>
拥2180亿美元收入,苹果成全球最大IT企业
查看>>