C # Simplified Chinese to traditional Chinese implementation code

Source: Internet
Author: User

Method 1:

Copy codeThe Code is as follows: // <summary>
/// Chinese Character tool class
/// </Summary>
Private const int LOCALE_SYSTEM_DEFAULT = 0x0800;
Private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
Private const int LCMAP_TRADITIONAL_CHINESE = 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>
/// Convert characters to simplified Chinese
/// </Summary>
/// <Param name = "source"> enter the string to be converted </param>
/// <Returns> string after conversion </returns>
Public static string ToSimplified (string source ){
String target = new String ('', source. Length );
Int ret = LCMapString (LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source. Length, target, source. Length );
Return target;
}

/// <Summary>
/// Convert the lecture character to traditional Chinese
/// </Summary>
/// <Param name = "source"> enter the string to be converted </param>
/// <Returns> string after conversion </returns>
Public static string ToTraditional (string source)
{
String target = new String ('', source. Length );
Int ret = LCMapString (LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source. Length, target, source. Length );
Return target;
}

Call the ToTraditiona method above and it will be OK ~ In addition, the same method of use ~
Method 2: (recommended)
① Right-click "add reference" in the corresponding folder in solution manager ---- select Microsoft. VisualBasic under. net reference;
② Add the namespace using Microsoft. VisualBasic to the aspx. cs file you want to implement the conversion function.
③ The following method can be used to directly implement conversion, which is very convenient! One sentence is enough ~ Therefore, this method is recommended.Copy codeThe Code is as follows: string s = "traditional ";
S = Strings. StrConv (s, VbStrConv. Wide, 0); // halfwidth to fullwidth
S = Strings. StrConv (s, VbStrConv. TraditionalChinese, 0); // convert simplified to traditional
S = Strings. StrConv (s, VbStrConv. ProperCase, 0); // uppercase
S = Strings. StrConv (s, VbStrConv. Narrow, 0); // returns the halfwidth.
S = Strings. StrConv (s, VbStrConv. SimplifiedChinese, 0); // convert traditional to simplified

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.