ASP. NET conversion of Chinese characters to PinYin-specific implementation of retrieving Chinese Characters

Source: Internet
Author: User

Not long ago, I saw some friends entering the product name in pinyin, and found that his implementation method was manually input.-_-#, comrades, welfare is coming!

Microsoft provides Microsoft Visual Studio International Pack for developers to convert their International languages. The Pack contains language packs for Chinese, Japanese, Korean, English, and other countries, it also provides methods for mutual conversion, Pinyin acquisition, Word Count acquisition, and even pen and video number acquisition.

The example here describes how to input Chinese characters, obtain their pinyin, and obtain the first letter of the pinyin as follows:

First, go to the Microsoft official website to download the Microsoft Visual Studio International Pack Language Pack, which is as follows:
Microsoft Visual Studio International Pack 1.0 SR1

Microsoft Visual Studio International Feature package 2.0

After the download, it is divided into two parts: “vsintlpack1.zip, "Vsintlpack2.msi", double-click "Vsintlpack2.msi" for installation, the path is random, but remember, because,
Install unzip vsintlpack2.msi”to decompress unzip vsintlpack1.zip. It contains seven language packs,
For example, convert Chinese characters to "CHSPinYinConv. msi", and convert traditional Chinese characters to "CHTCHSConv. msi ..

Here we use "CHSPinYinConv. msi", double-click the installation, open Visual Studio, create a WinForm project, and create a form layout, as shown in,
 

First, add a reference to the Language Pack you just installed:

"D: \ Program Files (x86) \ Microsoft Visual Studio International Pack \ Simplified Chinese Pin-Yin Conversion Library \ ChnCharInfo. dll"

The default disk is drive C. Here I installed it on drive D, and then added using reference:
Copy codeThe Code is as follows:
Using Microsoft. International. Converters. PinYinConverter; // import pinyin

Create a method to obtain pinyin:
Copy codeThe Code is as follows:
/// <Summary>
/// Convert Chinese characters to PinYin
/// </Summary>
/// <Param name = "str"> Chinese characters </param>
/// <Returns> full competition </returns>
Public static string GetPinyin (string str)
{
String r = string. Empty;
Foreach (char obj in str)
{
Try
{
ChineseChar chineseChar = new ChineseChar (obj );
String t = chineseChar. Pinyins [0]. ToString ();
R + = t. Substring (0, t. Length-1 );
}
Catch
{
R + = obj. ToString ();
}
}
Return r;
}

Create a method to obtain the first letter of Chinese pinyin:
Copy codeThe Code is as follows:
/// <Summary>
/// Convert Chinese characters to the first letter of the pinyin alphabet
/// </Summary>
/// <Param name = "str"> Chinese characters </param>
/// <Returns> initial letter </returns>
Public static string GetFirstPinyin (string str)
{
String r = string. Empty;
Foreach (char obj in str)
{
Try
{
ChineseChar chineseChar = new ChineseChar (obj );
String t = chineseChar. Pinyins [0]. ToString ();
R + = t. Substring (0, 1 );
}
Catch
{
R + = obj. ToString ();
}
}
Return r;
}

Then, call the above method in the Click Event of the "Convert pinyin" button:
Copy codeThe Code is as follows:
// Convert Chinese characters to PinYin
Private void btn_One_Click (object sender, EventArgs e)
{
String source = this.txt _ ChineseCharacter_One.Text.Trim (); // obtain the Input source character
String result = GetPinyin (source); // call the method to obtain pinyin
This.txt _ Pinyin_One.Text = result;
}

Click the "First Letter" button to call the above method in the event:
Copy codeThe Code is as follows:
// First letter
Private void btn_Two_Click (object sender, EventArgs e)
{
String source = this.txt _ ChineseCharacter_One.Text.Trim (); // obtain the Input source character
String result = GetFirstPinyin (source); // call the method to obtain pinyin
This.txt _ Pinyin_One.Text = result;
}

By now, you have finished 80% and run the program. You will find that when you click "Convert pinyin", the result is as follows:

It's not the effect of "Gu Ying" that I started to talk about. This is because I simply handled it when getting pinyin:
Copy codeThe Code is as follows:
// Convert Chinese characters to PinYin
Private void btn_One_Click (object sender, EventArgs e)
{
String source = this.txt _ ChineseCharacter_One.Text.Trim (); // obtain the Input source character

String result = string. Empty; // PinYin conversion result
String temp = string. Empty; // The temporary variables used by foreach below
Foreach (char item in source) // traverses each source character
{
Temp = GetPinyin (item. ToString (); // convert each character to PinYin
// Processing: Obtain uppercase letters and lowercase letters
Result + = (String. format ("{0} {1}", temp. substring (0, 1 ). toUpper (), temp. substring (1 ). toLower ()));
}

// String result = GetPinyin (source); // call the method to obtain pinyin
This.txt _ Pinyin_One.Text = result;
}

OK. At this point, this function has been implemented, and Other Language Pack functions are similar to this function, you can use Microsoft Visual Studio International Pack, exchange between languages, and functional examples.

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.