mirror of
https://gitee.com/xiarenalofs/squad-rain-ops-mini.git
synced 2026-08-07 05:45:46 +08:00
65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using System;
|
|
using System.Text;
|
|
using NPinyin;
|
|
|
|
namespace RainOpsMini.Helpers
|
|
{
|
|
public static class PinyinHelper
|
|
{
|
|
/// <summary>
|
|
/// 获取字符串的首字母(拼音首字母)
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <returns></returns>
|
|
public static string GetInitials(string str)
|
|
{
|
|
if (string.IsNullOrEmpty(str)) return "";
|
|
try
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
foreach (char c in str)
|
|
{
|
|
// If Chinese
|
|
if (c >= 0x4e00 && c <= 0x9fa5)
|
|
{
|
|
var pinyin = Pinyin.GetPinyin(c);
|
|
if (!string.IsNullOrEmpty(pinyin))
|
|
{
|
|
sb.Append(pinyin[0]);
|
|
}
|
|
else
|
|
{
|
|
sb.Append(c);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sb.Append(c);
|
|
}
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
catch
|
|
{
|
|
return str; // Fallback
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全拼
|
|
/// </summary>
|
|
public static string GetPinyin(string str)
|
|
{
|
|
if (string.IsNullOrEmpty(str)) return "";
|
|
try
|
|
{
|
|
return Pinyin.GetPinyin(str).Replace(" ", "");
|
|
}
|
|
catch
|
|
{
|
|
return str;
|
|
}
|
|
}
|
|
}
|
|
}
|