using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.International.Converters.PinYinConverter;
using System.Collections.ObjectModel;
namespace 汉字转拼音练习
{
class Program
{
static void Main(string[] args)
{
string str = " 张三李四abd ";
string s = ChangePin(str);
Console.WriteLine(s);
Console.ReadKey();
}
//定义一个方法用来把传入的字符串转成拼音
static string ChangePin(string str)
{
//判断一个字符串是否是汉字
//char ch=' ';
string s = "";
for (int i = 0; i < str.Length; i++)
{
if (ChineseChar.IsValidChar(str[i]))//判断是不是汉字 如果不是则原样输出
{
ChineseChar cc = new ChineseChar(str[i]);//创建一个转换汉字的实例
ReadOnlyCollection<string> list = cc.Pinyins;//转换汉字 得到的是一个集合
//for (int j = 0; j < list.Count; j++)
//{
// Console.WriteLine(list[j]);
//}
//Console.WriteLine("**");
s += list[0].Substring(0, list[0].Length - 1);//得到需要的拼音 这里要不停的去尝试查看一下
}
else
{
//return str[i].ToString();
s += str[i].ToString();
}
}
return s;
}
}
}
汉字转拼音练习
最新推荐文章于 2021-09-28 16:48:26 发布