C # How to scale out a solution I: Chinese Processing

Source: Internet
Author: User
Disclaimer: This article only provides a programming idea, and the Code provided is for reference only. If you need to use it, please complete it yourself.

We often need to process user input when making programs. as the main language of Chinese, full-width and half-width problems often occur, which will cause us a lot of trouble during query.
This article uses an extension method to solve the problem of full-width switching,

The Code is as follows: Chinese processing Extension
Public static void example (string userinput)
{
String SBC = userinput. tosbc (); // returns the fullwidth
// Specific operations, such as database storage
String DBC = userinput. todbc (); // returns the halfwidth.
// Specific operations, such as database storage
}
/** // <Summary>
/// Convert the fullwidth (SBC case)
/// </Summary>
/// <Param name = "input"> any string </param>
/// <Returns> fullwidth string </returns>
Public static string tosbc (this string input)
{
Char [] C = input. tochararray ();
For (INT I = 0; I <C. length; I ++)
{
If (C [I] = 32)
{
C [I] = (char) 12288;
Continue;
}
If (C [I] <127)
C [I] = (char) (C [I] + 65248 );
}
Return new string (C );
}
/** // <Summary>
/// Turn the halfwidth (DBC case)
/// </Summary>
/// <Param name = "input"> any string </param>
/// <Returns> halfwidth string </returns>
Public static string todbc (this string input)
{
Char [] C = input. tochararray ();
For (INT I = 0; I <C. length; I ++)
{
If (C [I] = 12288)
{
C [I] = (char) 32;
Continue;
}
If (C [I]> 65280 & C [I] <65375)
C [I] = (char) (C [I]-65248 );
}
Return new string (C );
}

Another commonly used Chinese processing method is to obtain the first letter of the Chinese alphabet, for example, the People's Republic of China, with the first letter zhrmghg. In many inventory management programs, this is used as a help code for fast query.
Only the extended definitions and examples are provided here. There are many implementations on the Internet, but most of them cannot solve the problem of multiphoneme. Code
Public static void example2 (string productname)
{
String shortname = productname. getchinesespell ();
}

/** // <Summary>
/// Obtain the first letter of the Chinese alphabet
/// </Summary>
/// <Param name = "S"> input </param>
/// <Returns> the first letter of the Chinese pinyin alphabet. Other characters are returned as they are </returns>
Public static string getchinesespell (this string S)
{
Return NULL; // complete it by yourself
}

You can also extend the conversion between simplified and traditional Chinese characters.
If you have any new ideas about the expansion of Chinese processing, please submit them.
The C # extension method is a special topic and contains many applications. Please stay tuned! Thank you!

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.