C# 汉字转化拼音的简单实例代码

首先引入ChnCharInfo.dll 第3方的一个库

代码:

btn_chinChar_Click事件:


代码如下:

private void btn_chinChar_Click(object sender, EventArgs e)
        {
            ChineseChar cr =null;
            string str = "", txtString = txt_string.Text.Trim();
            if (!string.IsNullOrEmpty(txtString))
            {
                foreach (var item in cr.GetChineseSpellings(txtString))
                {
                    str += item + "-";
                }
            }
            MessageBox.Show(str);
        }

扩展方法:

代码如下:

public static class ChineseCharacters
    {
        public static ICollection<string> GetChineseSpellings(this ChineseChar chinChar ,string value)
        {
            List<string> list;
            int i,start;
            char c;

if (string.IsNullOrEmpty(value))return null;

start = 0;
            list = new List<string>();

for (i = 0; i < value.Length; ++i)
            {
                c = value[i];
                if (ChineseChar.IsValidChar(c))
                {
                    if (i > start)
                    {
                        list.Add(value.Substring(start, i - start));
                    }
                    chinChar = new ChineseChar(c);
                    list.Add(chinChar.Pinyins.First().Substring(0, chinChar.Pinyins.First().Length - 1).ToLower());
                    start = i + 1;
                }
            }

if (i > start)
            {
                list.Add(value.Substring(start, i - start));
            }
            return list;
        }
    }

运用结果:

(0)

相关推荐

  • C#将Unicode编码转换为汉字字符串的简单方法

    C# 将js中的UNICODE转换为字符串,网上找的都不行,遇到有数字的转不出来,稍稍改了点,OK了! 实例如下: /// 将Unicode编码转换为汉字字符串 /// /// Unicode编码字符串 /// 汉字字符串 public static string ToGB2312(string str) { MatchCollection mc = Regex.Matches(str, "([\\w]+)|(\\\\u([\\w]{4}))"); if (mc != null &am

  • C#把UNICODE编码转换为GB编码的实例

    实例如下: public string unicodetogb(string text) { System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(text, "\\\\u([\\w]{4})"); if (mc != null && mc.Count > 0) { foreach (System.Text.RegularEx

  • C#实现汉字转换为拼音缩写的代码

    本文实例为大家分享了C#汉字转换为拼音缩写的实现代码,供大家参考,具体内容如下 using System; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web

  • .Net(c#)汉字和Unicode编码互相转换实例

    {"Tilte": "\u535a\u5ba2\u56ed", "Href": "http://www.jb51.net"} 经常遇到这样内容的json字符串,原来是把其中的汉字做了Unicode编码转换. Unicode编码: 将汉字进行UNICODE编码,如:"王"编码后就成了"\王",UNICODE字符以\u开始,后面有4个数字或者字母,所有字符都是16进制的数字,每两位表示的25

  • C# 汉字转化拼音的简单实例代码

    首先引入ChnCharInfo.dll 第3方的一个库 代码: btn_chinChar_Click事件: 复制代码 代码如下: private void btn_chinChar_Click(object sender, EventArgs e)        {            ChineseChar cr =null;            string str = "", txtString = txt_string.Text.Trim();            if

  • MySQL按照汉字的拼音排序简单实例

    如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字3755个采用拼音排序,二级汉字就不是了,但考虑到人名等都是常用汉字,因此只是针对一级汉字能正确排序也够用了). 直接在查询语句后面 添加 order by name asc; 查询结果按照姓氏的升序排序: 如果存储姓名的字段采用的是 utf8字符集,需要在排序的时候对字段进行转码:对于的代码是  order by convert(name using gbk) asc; 同样,查询的

  • Yii2汉字转拼音类的实例代码

    类核心代码: <?php namespace mobile\models; use Yii; use yii\base\Model; /** * 汉字转拼音 * @property integer */ class SpellModel extends Model { /** * 拼音字符转换图 * @var array */ private static $_aMaps = array( 'a'=>-20319,'ai'=>-20317,'an'=>-20304,'ang'=&g

  • Java web的读取Excel简单实例代码

    目录结构: Data.xls数据: 后台页面: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //System.out.println(this.getServletContext().getRealPath ("/")); try{ Workbook wb = Workbook.getWorkbook(

  • 分享Ajax创建简单实例代码

    XmlHttp是一套可以在Javascript.VbScript.Jscript等脚本语言中通过http协议传送或从接收XML及其他数据的一套API.XmlHttp最大的用处是可以更新网页的部分内容而不需要刷新整个页面.几乎所有的浏览器都支持XMLHttpRequest对象,它是Ajax应用的核心技术. js代码如下: <html> <head> <title> New Document </title> <meta charset="utf

  • 仿百度换肤功能的简单实例代码

    效果:(换肤出来一个div,选择你想要的图片,作为网页背景,保存) 要点:cookie保存状态 html代码: <body> <div id="header"> <div id="header_con"> <div class="dbg"><a href="javascript:;" onclick="showImgBox()">换肤</a&

  • matplotlib简介,安装和简单实例代码

    官网介绍: Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shell, the ju

  • Vue验证码60秒倒计时功能简单实例代码

    template <template> <div class='login'> <div class="loginHeader"> <input type="tel" class="loginBtn border-bottom" placeholder="请输入手机号" /> <input type="tel" class="codeBtn&q

  • Java汉字转拼音工具类完整代码实例

    添加依赖 <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency> 工具类代码: public class PinYinUtils { public static HanyuPinyinOutputFormat PINYIN_FORMA

  • Java中汉字转拼音pinyin4j用法实例分析

    本文实例讲述了Java中汉字转拼音pinyin4j用法.分享给大家供大家参考,具体如下: 汉字转换拼音在日常开发中是个很常见的问题.例如我们伟大的12306,在地名中输入"WH",就会出现"武汉""芜湖""威海"等地名,输入"WUHU"就会出现"芜湖". Java获取汉字的拼音,pinyin4j这个库可以很好的解决这个问题. 下载地址:http://sourceforge.net/pro

随机推荐