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

引入NPinyin

using NPinyin;

调用

 /// <summary>
/// 中文首字母大写
/// </summary>
/// <param name="str">中文</param>
/// <returns></returns>
public String GetSpellCode(string str)
{
Encoding gb2312 = Encoding.GetEncoding("GB2312");
string strA = Pinyin.ConvertEncoding(str, Encoding.UTF8, gb2312);
//首字母
string strB = Pinyin.GetInitials(strA, gb2312);
       //拼音
       //string strC = Pinyin.GetPinyin(str);
return strB; }

报错

Unhandled Exception: System.ArgumentException: 'GB2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

解决

原因

使用如下代码检查支持的编码:

System.Text.Encoding.GetEncodings();

发现获得的编码中没有GB2312或者GBK。

解决方案

1.

第一步

向项目中添加如下包:

System.Text.Encoding.CodePages

根据 System.Text.Encoding.CodePages nuget主页 的描述,这个包能为程序提供 Windows-1252, Shift-JIS, and GB2312 三种编码。

第二步

根据错误提示,我们需要对引用的编码使用 Encoding.RegisterProvider 函数进行注册。

在使用 System.Text.Encoding.GetEncoding ("GB2312") 之前,在代码中执行:

System.Text.Encoding.RegisterProvider (System.Text.CodePagesEncodingProvider.Instance);

注册完之后,获取 GB2312 编码对象就不会报错了,并且可以正常使用其中的函数。

完整代码是:

/// <summary>
/// 中文首字母大写
/// </summary>
/// <param name="str">中文</param>
/// <returns></returns>
public String GetSpellCode(string str)
{
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);//注册编码对象
Encoding gb2312 = Encoding.GetEncoding("GB2312");
string strA = Pinyin.ConvertEncoding(str, Encoding.UTF8, gb2312);
//首字母
string strB = Pinyin.GetInitials(strA, gb2312);
      //拼音 
       //string strC = Pinyin.GetPinyin(str);
      return strB; 
    }

参考:https://blog.csdn.net/qq_22103321/article/details/85863879

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

  1. ASP.NET获取汉字拼音的首字母

    代码 #region GetChineseSpell获取汉字拼音的第一个字母 //获取汉字拼音的第一个字母 static public string GetChineseSpell(string st ...

  2. MySQL数据库获取汉字拼音的首字母函数

    需求简介:最近的一个项目,想实现如下图所示的显示效果.很明显,如果能够获取对应的汉字词组的拼音首字母就可以实现了,如果是固定的几个汉字,人为的拼一下就可以了,不过项目中有多处功能是需要这个效果的,并且 ...

  3. sqlserver 获取汉字拼音的首字母(大写)函数

    1:创建函数: USE [test] GO /****** 对象: UserDefinedFunction [dbo].[GetFirstChar] 脚本日期: 02/22/2019 16:39:06 ...

  4. oracle函数获取汉字拼音的首字母

    CREATE OR REPLACE FUNCTION F_TRANS_PINYIN_CAPITAL(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS V_COMPARE V ...

  5. MySQL数据库获取多个汉字拼音的首字母函数

    需求简介:最近的一个项目,想实现如下图所示的显示效果.很明显,如果能够获取对应的汉字词组中每个汉字的拼音首字母就可以实现了,如果是固定的几组汉字,人为的拼一下就可以 了,不过项目中有多处功能需要这个效 ...

  6. C# 汉字转拼音 取汉字拼音的首字母

    using System.Text.RegularExpressions; namespace DotNet.Utilities { /// <summary> /// 汉字转拼音类 // ...

  7. [功能帮助类] C#取汉字拼音的首字母PinYin帮助类 (转载)

    点击下载 PinYin.rar 主要功能就是取汉字拼音的首字母,只要你输入一个汉字,或者是多个汉字就会取出相应的道字母,主要是方便查询使用的 /// <summary> /// 编 码 人 ...

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

    现实中我们经常看到这样的说明,排名不分先后,按姓名首字母进行排序.这是中国人大多数使用的排序方法.那么在php程序中该如何操作呢? 下面就分享一下在php程序中获取汉字拼音的首字母的方法,在网上搜到的 ...

  9. 获取汉字拼音 Java

    两种方法:一个是使用btye数组,一个是引入jar包进行操作. 1. public class CharacterParser { private static int[] pyvalue = new ...

随机推荐

  1. hdoj - 1342 Lotto

    Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,... ...

  2. Join Reorder优化 - 论文摘要

    Query Simplification: Graceful Degradation for Join-Order Optimization 这篇的related work可以参考,列的比较全面, Q ...

  3. 支付宝即时到账交易接口C#接入方式的几个坑

    1.在官方文档中 https://docs.open.alipay.com/62/104743 可以清楚看到input_charset前面没有要求加下横杠,可是请求示例是带着的.经过实验得知,这个必须 ...

  4. 013-centos7 常用命令--查看当前用户的4种方法

    一.概述 4种查看系统用户信息(通过编号(ID))的方法. 1.1. 使用w命令查看登录用户正在使用的进程信息 w命令用于显示已经登录系统的用户的名称,以及他们正在做的事.该命令所使用的信息来源于/v ...

  5. [译]如何在红帽系统(RHEL)上源码安装python3?

    原文来源: https://stackoverflow.com/questions/8087184/installing-python-3-on-rhel 很容易手动安装. 1.下载对应的python ...

  6. PyTorch之DataLoader杂谈

    输入数据PipeLine pytorch 的数据加载到模型的操作顺序是这样的: ①创建一个 Dataset 对象②创建一个 DataLoader 对象③循环这个 DataLoader 对象,将img, ...

  7. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  8. [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  9. Spring boot后台搭建二为Shiro权限控制添加缓存

    在添加权限控制后,添加方法 查看 当用户访问”获取用户信息”.”新增用户”和”删除用户”的时,后台输出打印如下信息 , Druid数据源SQL监控 为了避免频繁访问数据库获取权限信息,在Shiro中加 ...

  10. 机器学习技法实现(一):AdaBoost- Decision Stump (AdaBoost - 决策树的基于Matlab的实现)

    经过前面对AdaBoost的总结,下面要基于Matlab实现AdaBoost-Stump进行二维平面数据点的分类的实验. 一. 实验原理 参看 http://blog.csdn.net/lg12591 ...