Skip to content

Commit c587a99

Browse files
author
DwGoing
committed
🦄 refactor(): 调整Pinyin相关实现
1 parent e01f204 commit c587a99

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/DwFramework.Core/DwFramework.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PackageReference Include="NLog.Extensions.Logging" Version="1.7.2"/>
1818
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="6.0.0"/>
1919
<PackageReference Include="RSAExtensions" Version="1.0.3"/>
20-
<PackageReference Include="CHSPinYinConv" Version="1.0.0"/>
2120
<PackageReference Include="K4os.Compression.LZ4.Streams" Version="1.2.6"/>
21+
<PackageReference Include="hyjiacan.pinyin4net" Version="4.1.0"/>
2222
</ItemGroup>
2323
</Project>

src/DwFramework.Core/Extensions/StringExtension.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.IO.Compression;
66
using System.Text.RegularExpressions;
77
using System.Threading.Tasks;
8-
using Microsoft.International.Converters.PinYinConverter;
8+
using hyjiacan.py4n;
99
using K4os.Compression.LZ4.Streams;
1010

1111
namespace DwFramework.Core
@@ -210,23 +210,28 @@ public static byte[] FromHex(this string hexString)
210210
/// </summary>
211211
/// <param name="char"></param>
212212
/// <returns></returns>
213-
public static bool IsChinese(this char @char)
213+
public static bool IsChinese(this char @char) => PinyinUtil.IsHanzi(@char);
214+
215+
/// <summary>
216+
/// 获取中文字符的拼音首字母
217+
/// </summary>
218+
/// <param name="@char"></param>
219+
/// <returns></returns>
220+
public static string GetFirstPinyin(this char @char)
214221
{
215-
var pattern = @"^[\u4e00-\u9fa5]$";
216-
if (Regex.IsMatch(@char.ToString(), pattern)) return true;
217-
return false;
222+
if (!IsChinese(@char)) return null;
223+
return Pinyin4Net.GetFirstPinyin(@char, PinyinFormat.UPPERCASE);
218224
}
219225

220226
/// <summary>
221227
/// 获取中文字符的拼音
222228
/// </summary>
223-
/// <param name="@char"></param>
229+
/// <param name="char"></param>
224230
/// <returns></returns>
225-
public static string[] GetPinYin(this char @char)
231+
public static string[] GetPinyin(this char @char)
226232
{
227233
if (!IsChinese(@char)) return null;
228-
var decoder = new ChineseChar(@char);
229-
return decoder.Pinyins;
234+
return Pinyin4Net.GetPinyin(@char, PinyinFormat.UPPERCASE);
230235
}
231236

232237
/// <summary>

0 commit comments

Comments
 (0)