之前做过一个程序,把名字转换为拼音。今天整理了出来。这个程序的关键点在于数据,可以从以下链接下载具体数据,内容为汉字的注音和解释。
链接: https://pan.baidu.com/s/1o98UE6m
密码: p7nt
准备工作:
先在数据库中建立一个表,我的数据库是SQL SERVER数据。建表语句如下:
CREATE TABLE [dbo].[ChineseToPinyin](
[F_GUID] [varchar](40) NULL,
[F_ZI] [nvarchar](30) NULL,
[F_PY] [varchar](200) NULL,
[F_WUBI] [varchar](200) NULL,
[F_BUSHOU] [nvarchar](100) NULL,
[F_BIHUA] [int] NULL,
[F_DESCRIPT] [nvarchar](2000) NULL,
[F_CONTENT] [nvarchar](2000) NULL
)
GO
ALTER TABLE [dbo].[ChineseToPinyin] ADD DEFAULT (newid()) FOR [F_GUID]
GO
转换方法如下:
// 参数说明:chinese 汉字字符串
// 返回值 :string[]数组长度为2;
// string[0]为汉字首字母;
// string[1]为汉字全拼。
// 如果为多音字则返回多个字符串用逗号隔开。
public static string[] getDatePinyin(string chinese)
{
string strConn = "数据库连接字符串";
string[] arrResult = new string[2];
try
{
if (chinese.Trim() != "")
{
string pinyin = "";
string first = "";
string zi = "";
string py = "";
List<string> lst = new List<string>();
List<string> lstFirst = new List<string>();
for (int i = 0; i < chinese.Length; i++)
{
zi = chinese.Substring(i, 1);
string sql = "select F_py from ChineseToPinyin where RTRIM(LTRIM(f_zi))='" + zi.Trim() + "'";
// 根据汉字查询对应的拼音
try
{
object obj = DataAccess.GetSingleSQL(sql, strConn);
if (obj != null)
{
py = obj.ToString().Trim();
}
else
{
// 如果未查询到对应的拼音则直接返回汉字本身
py = zi;
}
}
catch (Exception e)
{
// 如果抛出异常则直接返回汉字本身
py = zi;
}
if (py.Trim() != "")
{
string[] arr = null;
// 多音字的拼音会用逗号隔开
if (py.IndexOf(',') >= 0)
{
arr = py.Split(',');
}
else
{
arr = new string[1];
arr[0] = py;
}
// 由于是多个字循环,所以需要判断list中是否有内容
if (lst.Count > 0)
{
// 临时列表,用于存储拼音连接串
List<string> lstTmp = new List<string>();
List<string> lstFirstTmp = new List<string>();
for (int j = 0; j < arr.Length; j++)
{
if (arr[j] != null && arr[j].Trim() != "")
{
// 将原有的内容和新的字的拼音进行连接
for (int k = 0; k < lst.Count; k++)
{
lstTmp.Add(lst[k] + arr[j]);
lstFirstTmp.Add(lstFirst[k] + arr[j].Substring(0, 1));
}
}
}
lst.Clear();
lstFirst.Clear();
// 将临时列表的内容存储到列表中
for (int l = 0; l < lstTmp.Count; l++)
{
lst.Add(lstTmp[l]);
lstFirst.Add(lstFirstTmp[l]);
}
}
else
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[j] != null && arr[j].Trim() != "")
{
lst.Add(arr[j]);
lstFirst.Add(arr[j].Substring(0, 1));
}
}
}
}
}
first = "";
pinyin = "";
for (int iIndex = 0; iIndex < lst.Count; iIndex++)
{
if (pinyin == "")
{
pinyin = lst[iIndex];
}
else
{
pinyin = pinyin + "," + lst[iIndex];
}
}
for (int iIndex = 0; iIndex < lstFirst.Count; iIndex++)
{
if (first == "")
{
first = lstFirst[iIndex];
}
else
{
first = first + "," + lstFirst[iIndex];
}
}
arrResult[0] = first;
arrResult[1] = pinyin;
}
else
{
arrResult[0] = "";
arrResult[1] = "";
}
}
catch (Exception ex)
{
arrResult[0] = "";
arrResult[1] = "";
}
return arrResult;
}
然后直接调用就可以了。
知行办公,专业移动办公平台
https://zx.naton.cn/
【总监】十二春秋之,3483099@qq.com;
【Master】zelo,616701261@qq.com;
【运营】运维艄公,897221533@qq.com;
【产品设计】流浪猫,364994559@qq.com;
【体验设计】兜兜,2435632247@qq.com;
【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;
【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;
【java】首席工程师MR_W,feixue300@qq.com;
【测试】土镜问道,847071279@qq.com;
【数据】fox009521,42151960@qq.com;
【安全】保密,你懂的