-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
42 lines (36 loc) · 1.46 KB
/
StringExtensions.cs
File metadata and controls
42 lines (36 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Runtime.InteropServices;
namespace YourNameSpace
{
public static class StringExtensions
{
private const int LocaleSystemDefault = 0x0800;
private const int LcmapSimplifiedChinese = 0x02000000;
private const int LcmapTraditionalChinese = 0x04000000;
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int LCMapString(int locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);
/// <summary>
/// 轉簡體
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ToSimplified(this string ori)
{
if (string.IsNullOrEmpty(ori)) return ori;
var result = new string(' ', ori.Length);
LCMapString(LocaleSystemDefault, LcmapSimplifiedChinese, ori, ori.Length, result, ori.Length);
return result;
}
/// <summary>
/// 轉繁體
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ToTraditional(this string ori)
{
if (string.IsNullOrEmpty(ori)) return ori;
var result = new string(' ', ori.Length);
LCMapString(LocaleSystemDefault, LcmapTraditionalChinese, ori, ori.Length, result, ori.Length);
return result;
}
}
}