Support Traditional Simplified interchangeable.
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Runtime.InteropServices; 6 using System.Threading.Tasks; 7 8 namespace DLMApi.Utils 9 { 10 /// <summary> 11 /// 中文字符工具类 12 /// </summary> 13 public class ChineseStringUtility 14 { 15 private const int LOCALE_SYSTEM_DEFAULT = 0x0800; 16 private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000; 17 private const int LCMAP_TRADITIONAL_CHINESE = 0x04000000; 18 19 [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)] 20 private static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] String lpDestStr, int cchDest); 21 is 22 is /// <Summary> 23 is /// the simplified Chinese characters into 24 /// </ Summary> 25 /// <param name = "Source"> Enter the character to be converted string </ param> 26 is /// <Returns> string conversion is completed </ Returns> 27 public static string ToSimplified ( string Source) 28 { 29 string target = new new string ( ' ' , source.Length); 30 int ret =LCMapString to (LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, Source, source.Length, target, source.Length); 31 is return target; 32 } 33 is 34 is /// <Summary> 35 /// the traditional Chinese character into 36 /// </ Summary > 37 [ /// <param name = "Source"> input string to be converted </ param> 38 is /// <Returns> string conversion is completed </ Returns> 39 public static string ToTraditional ( string Source) 40 { 41 is String target = new newString ( ' ' , source.Length); 42 is int RET = LCMapString to (LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, Source, source.Length, target, source.Length); 43 is return target; 44 is } 45 46 is // <Summary> 47 /// obtaining a similarity of two strings 48 /// </ Summary> 49 /// <param name = "of sourceString"> of a string </ param> 50 /// <param name = "STR"> section two string </ param> 51 is public static int GetSimilarityWith ( stringof sourceString, String STR) 52 is { 53 is char [] SS = sourceString.ToCharArray (); 54 is char [] ST = str.ToCharArray (); 55 56 is // Get the number of intersection 57 is int Q = ss.Intersect (ST) .Count (); 58 return Q; 59 } 60 61 is } 62 is }