C#汉字转拼音
下面这个方法里 ' _Allhz' 是在什么地方声明的?
还有一个地方报错,因为给删了,想不起来了;
大家有用过这个方法的?
public static string ConvertPY(string SourceString)
{
if (SourceString == null)
return null;
Encoding ed = Encoding.GetEncoding("GB2312");
if (ed == null)
throw (new ArgumentException("没有找到编码集GB2312"));
int bh = 0;
char[] charary = SourceString.ToCharArray();
byte[] bAry = new byte[2];
StringBuilder rtnSb = new StringBuilder();
for (int i = 0; i < charary.Length; i++)
{
bAry = ed.GetBytes(charary[i].ToString());
if (bAry.Length == 1)
{
rtnSb.Append(charary[i]);
continue;
}
bh = bAry[0] - 0xA0;
if (0x10 <= bh && bh <= 0x57) //是gb2312汉字
{
bool isFind = false;
for (int j = 0; j < _Allhz.Length; j++)
{
if (_Allhz[j][1].IndexOf(charary[i]) != -1)
{
rtnSb.Append(_Allhz[j][0]);
isFind = true;
break;
}
}
if (!isFind)
rtnSb.Append(charary[i]);
}
else
rtnSb.Append(charary[i]);
}
return rtnSb.ToString();
}
------解决方案--------------------给你一段我自己用过的吧 net2003的代码 2005下也好用
//获取简体中文字符串拼音首字母
static public string getSpells(string input)
{
int len = input.Length;
string reVal = "";
for(int i=0;i<len;i++)
{
reVal += getSpell(input.Substring(i,1));
}
return reVal;
}
//获取一个简体中文字的拼音首字母
static public string getSpell(string cn)
{
byte[] arrCN = System.Text.Encoding.Default.GetBytes(cn);
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 System.Text.Encoding.Default.GetString(new byte[]{(byte)(65+i)});
}
}
return "?";
}
else return cn;
}
------解决方案--------------------/// <summary>
/// 汉字转拼音缩写
///<param name="str">要转换的汉字字符串</param>
///<returns>拼音缩写</returns>
public string GetPYString(string str)
{
string tempStr = "";
foreach(char c in str)
{
//if((int)c >= 33 && (int)c &l