请教:如何实现获取汉字的拼音首字母
求一个可以用来获取汉字的拼音首字母的方法,我也在其它几篇帖子里看到过类似问题的解答,可大部分都是通过输入法生成器转换出一个单字与拼音的对应文件来实现的,想请教各位看有没有其它的更高效的方法,请不吝赐教。
另外,如果使用输入法生成器这个方法,在VB.net中该如何实现,并且我需要达到以下要求:一串汉字字符串,其中既有汉字,也包含有英文字母/单词和数字,此外还可能包含空格和标点符号,我希望这个程序执行过程中,遇到汉字则获取汉字拼音的首个字母,遇到英文字母则直接获取该字母,遇到英文单词则获取该单词的首个字母,遇到空格和标点则不做处理而直接跳到下一个字符,从而最终形成一个与该字符串相对应的字母字符串。例如,字符串是“我平时主要使用Visual Basic6.0和.net,我正在学习C#和Java。”,经过处理得到这个-“wpszysyvb60hnwzzxxchj”。看来似乎英文的字母和单词是无法区分的,那么遇到英文时,无论是字母还是单词就都按原样提取吧。
请知者多费心,尽量详细的将实现方法和过程叙述下来,有劳!多谢!!!
[解决办法]
lizhizhe2000(武安侯) ( ) 信誉:100 Blog 加为好友 2007-3-17 9:04:53 得分: 20
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/*
* 说明:此类用于将一个字符串找出首字声母,用法:ChineseConvert.UtilIndexCode(str);
*
*
*/
/// <summary>
/// ChineseConvert 的摘要说明
/// </summary>
public class ChineseConvert
{
public ChineseConvert()
{
}
/// <summary>
/// 返回字符串的首写字母字符串
/// </summary>
/// <param name= "IndexTxt "> 需得到首写字母的字符串 </param>
/// <returns> </returns>
public static String UtilIndexCode(String IndexTxt)
{
string _Temp = null;
for (int i = 0; i < IndexTxt.Length; i++)
_Temp = _Temp + GetOneIndex(IndexTxt.Substring(i, 1));
return _Temp;
}
//得到单个字符的首字母
private static String GetOneIndex(String OneIndexTxt)
{
if (Convert.ToChar(OneIndexTxt) > = 0 && Convert.ToChar(OneIndexTxt) < 256)
return OneIndexTxt;
else
return GetGbkX(OneIndexTxt);
}
//根据汉字拼音排序得到首字母
private static string GetGbkX(string str)
{
if (str.CompareTo( "吖 ") < 0)
{
return str;
}
if (str.CompareTo( "八 ") < 0)
{
return "A ";
}
if (str.CompareTo( "嚓 ") < 0)
{
return "B ";
}
if (str.CompareTo( "咑 ") < 0)
{
return "C ";
}
if (str.CompareTo( "妸 ") < 0)
{
return "D ";
}
if (str.CompareTo( "发 ") < 0)
{
return "E ";
}
if (str.CompareTo( "旮 ") < 0)
{
return "F ";
}
if (str.CompareTo( "铪 ") < 0)
{
return "G ";
}
if (str.CompareTo( "讥 ") < 0)
{
return "H ";
}
if (str.CompareTo( "咔 ") < 0)
{
return "J ";
}
if (str.CompareTo( "垃 ") < 0)
{
return "K ";
}
if (str.CompareTo( "嘸 ") < 0)
{
return "L ";
}
if (str.CompareTo( "拏 ") < 0)
{
return "M ";
}
if (str.CompareTo( "噢 ") < 0)
{
return "N ";
}
if (str.CompareTo( "妑 ") < 0)
{
return "O ";
}
if (str.CompareTo( "七 ") < 0)
{
return "P ";
}
if (str.CompareTo( "亽 ") < 0)
{
return "Q ";
}
if (str.CompareTo( "仨 ") < 0)
{
return "R ";
}
if (str.CompareTo( "他 ") < 0)
{
return "S ";
}
if (str.CompareTo( "哇 ") < 0)
{
return "T ";
}
if (str.CompareTo( "夕 ") < 0)
{
return "W ";
}
if (str.CompareTo( "丫 ") < 0)
{
return "X ";
}
if (str.CompareTo( "帀 ") < 0)
{
return "Y ";
}
if (str.CompareTo( "咗 ") < 0)
{
return "Z ";
}
return str;
}
}
[解决办法]
http://www.cnblogs.com/XingfuStar/archive/2006/06/19/429641.html
[解决办法]
http://blog.csdn.net/dyc13/archive/2006/09/01/1154894.aspx
[解决办法]
楼上的方法楼主改一下就OK了,因为你要的是每个英文单词只取一个字母,上面的方法除汉字外数字字母都照取下来.
[解决办法]
Private Function GetChineseSpell(ByVal strText As String)
Dim Len As Integer
Dim myStr As String
Dim Index As Integer
Index = 0
myStr = String.Empty
Len = strText.Length
For Index = 0 To Len - 1 Step 1
myStr &= getSpell(strText.Substring(Index, 1))
Next
Return myStr
End Function
Private Function getSpell(ByVal cnChar As String)
Dim arrCN As Byte() = System.Text.Encoding.Default.GetBytes(cnChar)
If arrCN.Length > 1 Then
Dim area As Integer
Dim pos As Integer
Dim code As Integer
area = CShort(arrCN(0))
pos = CShort(arrCN(1))
code = (area < < 8) + pos
Dim areacode As Integer() = {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}
Dim Index As Integer
Index = 0
For Index = 0 To 25 Step 1
Dim max As Integer
max = 55290
If Not Index = 25 Then
max = areacode(Index + 1)
End If
If areacode(Index) <= code And code < max Then
Return System.Text.Encoding.Default.GetString(New Byte() {CByte(Index + 65)})
End If
Next
Return "* "
Else
Return cnChar
End If
End Function