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 = ;
for (i = ; i <= iLen - ; i++)
{
strTemp += GetCharSpellCode(CnStr.Substring(i, ));
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 == )
{
return CommonMethod.CutString(CnChar.ToUpper(),);
}
else
{
// get the array of byte from the single char
int i1 = (short)(ZW[]);
int i2 = (short)(ZW[]);
iCnChar = i1 * + i2;
}
// iCnChar match the constant
if ((iCnChar >= ) && (iCnChar <= ))
{
return "A";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "B";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "C";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "D";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "E";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "F";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "G";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "H";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "J";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "K";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "L";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "M";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "N";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "O";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "P";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "Q";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "R";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "S";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "T";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "W";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "X";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
return "Y";
}
else if ((iCnChar >= ) && (iCnChar <= ))
{
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 == || len <= )
{
return string.Empty;
} int l = str.Length; #region 计算长度
int clen = ;
while (clen < len && clen < l)
{
//每遇到一个中文,则将目标长度减一。
if ((int)str[clen] > ) { len--; }
clen++;
}
#endregion if (clen < l)
{
return str.Substring(, 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 = ;
int currLength = ;
int code = ;
int chfrom = Convert.ToInt32("4e00", ); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", );
for (int i = ; i < strContent.Length; i++)
{
code = Char.ConvertToUtf32(strContent, i);
if (code >= chfrom && code <= chend)
{
currLength += ; //中文
}
else
{
currLength += ;//非中文
}
splitLength = i + ;
if (currLength >= length)
{
isConvert = true;
break;
}
}
if (isConvert)
{
return strContent.Substring(, splitLength);
}
else
{
return strContent;
}
} public static int GetStringLenth(object content)
{
string strContent = NoHTML(content.ToString());
int currLength = ;
int code = ;
int chfrom = Convert.ToInt32("4e00", ); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", );
for (int i = ; i < strContent.Length; i++)
{
code = Char.ConvertToUtf32(strContent, i);
if (code >= chfrom && code <= chend)
{
currLength += ; //中文
}
else
{
currLength += ;//非中文
} }
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

public static string GetSpellAllCode(string CnStr)
{ return NPinyin.Pinyin.GetPinyin(CnStr); }
上述代码获取的全拼有空格分隔,如果你不需要空格,可采用:.Replace(" ", "") 把空格替换掉!
但是有个问题需要完善下:
如果上述获取首字母的方法得到的结果中包含‘?’号,这就说明上述方法存在不能翻译的汉字,这时候我们可以将二者相结合,如下:
string szm = PingYinHelper.GetSpellCode(smSupplierModel.SupplierName);
if (szm.Contains("?"))
{
szm = string.Empty;
var ary = PingYinHelper.GetSpellAllCode(smSupplierModel.SupplierName).Split(' ');
foreach (var py in ary)
{
szm += py.Substring(, );
} }
string supplierSZM = szm.ToUpper() + "_" + PingYinHelper.GetSpellAllCode(smSupplierModel.SupplierName).Replace(" ", ""); smSupplierEntity.Relatcpyno = supplierSZM;
@陈卧龙的博客
C# 获取汉字拼音首字母的更多相关文章
- C# 获取汉字拼音首字母/全拼
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节探讨C#获取汉字拼音首字母的方法: 代码类东西, ...
- java获取汉字拼音首字母 --转载
在项目中要更能根据某些查询条件(比如姓名)的首字母作为条件进行查询,比如查一个叫"李晓明"的人,可以输入'lxm'.写了一个工具类如下: import java.io.Unsupp ...
- JAVA获取汉字拼音首字母
package com.common.util; import java.io.UnsupportedEncodingException; /** * 取得给定汉字串的首字母串,即声母串 * Titl ...
- php获取汉字拼音首字母的方法
现实中我们经常看到这样的说明,排名不分先后,按姓名首字母进行排序.这是中国人大多数使用的排序方法.那么在php程序中该如何操作呢? 下面就分享一下在php程序中获取汉字拼音的首字母的方法,在网上搜到的 ...
- C/C++ 获取汉字拼音首字母
#include <stdint.h> #include <stdio.h> #include <ctype.h> #include <string.h> ...
- qt 获取汉字拼音首字母
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>#include ...
- php 获取汉字拼音首字母的函数
function getFirstChar($string){ if($string{0}>="A" and $string{0}<="z" )re ...
- 获取汉字拼音&首字母
pinyin4j https://www.cnblogs.com/yjq520/p/7681537.html
- PHP获取汉字拼音首字母 截取中文字符串
http://blog.csdn.net/everything1209/article/details/39005785 substr是按字符分割,而mb_strcut是按字节来分割,但是都不会产生半 ...
随机推荐
- Requests库练习
预备知识 字符串方法 用途 string.partition(str) 有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 ...
- YARN-MR 大数据第二天
大数据第二天 1.YARN(管理和调度集群中的各类资源) 1.1 产生的背景: 1.直接源于MR版本1的缺陷(如:单点故障.难以支持MR之外的计算框架等) 2.多计算框架各自为站,数据共享困难(如:M ...
- 坐标系统与投影变换及在ARCGIS中的应用
首先提几个问题:是否有遇到坐标转换的问题?又是否有遇到投影转换的问题?坐标转换与投影转换有什么区别?下面看几个概念:1.地球椭球体( Ellipsoid,Spheroid)2.大地基准面( Geo ...
- Daily Scrum Meeting ——ZeroDay(Beta)12.08
算是BETA冲刺的前奏,不算正式冲刺
- Linux install SMplayer
sudo apt-add-repository ppa:rvm/smplayer sudo apt-get update sudo apt-get install smplayer smplayer- ...
- phonegap3.5了结
搞了三天的phonegap3.5.最后怎么搞都搞不好了.修改了www中的index.html的内容,clean也不行,在node.js中cordova build也不行. 反正就是apk没有更新啦.至 ...
- RAC初体验
什么是RAC? 几乎每一篇介绍RAC的文章开头都是这么一个问题.我这篇文章是写给新手(包括我自己)看的,所以这个问题更是无法忽视. 简单的说,RAC就是一个第三方库,他可以大大简化你的代码过程. 官方 ...
- JSF标签的使用2
n 事件监听器是用于解决只影响用户界面的事件 Ø 特别地,在beans的form数据被加载和触发验证前被调用 • 用immediate=“true”指明这个行为不触发验证 Ø 在监听器调用 ...
- mysql数据库迁移
由于yum安装mysql的时候,数据库的data目录默认是在/var/lib下,出于数据安全性的考虑需要把它挪到/data分区.步骤如下: 一.关闭apache和mysql. 复制代码 代码如下: s ...
- 利用其它带文件防护功能的软件防止*.asp;*.jpg写入文件。
此木马是一个.NET程序制作,如果你的服务器支持.NET那就要注意了,,进入木马有个功能叫:IIS Spy,点击以后可以看到所有站点所在的物理路径.以前有很多人提出过,但一直没有人给解决的答案.. 防 ...