NewGaoKaoApi/New_College.Common/Helper/GetFirstLetterHelper.cs

32 lines
979 B
C#

using hyjiacan.py4n;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace New_College.Common.Helper
{
public static class GetFirstLetterHelper
{
public static string GetFirstLetterOfChinese(string chinese)
{ // 设置拼音输出格式
PinyinFormat format = PinyinFormat.WITHOUT_TONE | PinyinFormat.LOWERCASE | PinyinFormat.WITH_U_UNICODE;
if (string.IsNullOrWhiteSpace(chinese))
{
return string.Empty;
}
char china = chinese.Trim()[0];
// 取指定汉字的唯一或者第一个拼音
var pinyin =Pinyin4Net.GetFirstPinyin(china, format);
if (pinyin != null && pinyin.Length > 0)
{
return pinyin[0].ToString().ToUpper(); // 取首字母并转为大写
}
return string.Empty;
}
}
}