首先安装依赖包PinYinConverterCore
/// <summary>
/// 获取全拼(我爱你中国=>WOAINIZHONGGUO)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetFull(string str)
{
string PYstr = "";
foreach (char item in str.ToCharArray())
{
if (Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(item))
{
Microsoft.International.Converters.PinYinConverter.ChineseChar cc = new Microsoft.International.Converters.PinYinConverter.ChineseChar(item);
PYstr += cc.Pinyins[0].Substring(0, cc.Pinyins[0].Length - 1);
}
else
{
PYstr += item.ToString();
}
}
return PYstr;
}
/// <summary>
/// 获取首字符(我爱你中国=>WANZG)
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetFrist(string str)
{
string PYstr = "";
foreach (char item in str.ToCharArray())
{
if (Microsoft.International.Converters.PinYinConverter.ChineseChar.IsValidChar(item))
{
Microsoft.International.Converters.PinYinConverter.ChineseChar cc = new Microsoft.International.Converters.PinYinConverter.ChineseChar(item);
PYstr += cc.Pinyins[0][0];
}
else
{
PYstr += item.ToString()[0];
}
}
return PYstr;
}
- 本文标题: NETCore字符串转全拼或字符串首拼
- 文章分类:【.NET/Web】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
- 上一篇:C# 之int[]数组转字符串
- 下一篇: Vue 3.0自定义表单验证规则