Reproduced http://www.cnblogs.com/08shiyan/archive/2011/06/04/2072492.html
C # use Microsoft's Visual Studio International Pack class library to extract the first letter of Chinese pinyin
Yesterday, a user reminded me that the method for extracting Chinese characters and pinyin can use Microsoft's class library Visual Studio International Pack. Today I tried it and it is really easy to use! The usage is as follows:
First download Visual Studio International Pack 2.0, official: http://www.microsoft.com/downloads/zh-cn/details.aspx? FamilyID = 7D1DF9CE-4AEE-467F-996E-BEC826C5DAA2.
1.0 SR1:
Http://www.microsoft.com/zh-cn/download/details.aspx? Id = 15251
After the download is complete, install the run file VSIPSetup. msi (the default installation directory on the x86 operating system is C: \ Program Files \ Microsoft Visual Studio International Feature Pack 2.0 \). After the installation is complete, you need to add references in VS to reference: C: \ Program Files \ Microsoft Visual Studio International Pack \ Simplified Chinese Pin-Yin Conversion Library (pinyin) and C: \ Program Files \ Microsoft Visual Studio International Pack \ Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool (simple Conversion) dll can be used.
After completing the above work, the usage is very simple. Let's look at the code below:
Using Microsoft. International. Converters. PinYinConverter; // import pinyin
Namespace WebApplication2
{
Public class Class1
{
/// <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;
}
/// <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;
}
}
}
Call method: (reference first)
GetPinyin ("Wind shadows"); // obtain the full spelling
GetFirstPinyin ("wind shadow"); // get the first letter
Is it very simple? With this class library, it is much easier! By the way, I would like to add some simplified and simplified conversion methods, which may be used in some cases:
Pilot Import
Using Microsoft. International. Converters. TraditionalChineseToSimplifiedConverter;
/// <Summary>
/// Convert simplified to traditional
/// </Summary>
/// <Param name = "str"> simplified Chinese character </param>
/// <Returns> traditional Chinese characters </returns>
Public static string GetTraditional (string str)
{
String r = string. Empty;
R = ChineseConverter. Convert (str, ChineseConversionDirection. SimplifiedToTraditional );
Return r;
}
/// <Summary>
/// Convert traditional Chinese to simplified Chinese
/// </Summary>
/// <Param name = "str"> traditional Chinese character </param>
/// <Returns> simplified Chinese character </returns>
Public static string GetSimplified (string str)
{
String r = string. Empty;
R = ChineseConverter. Convert (str, ChineseConversionDirection. TraditionalToSimplified );
Return r;
}
Function overview of this class library
Visual Studio International Feature Pack 1.0 provides the following features:
- East Asia Numeric Formatting Library-supports Formatting lowercase Numeric strings into uppercase Numeric strings in simplified Chinese, traditional Chinese, Japanese, and Korean.
- Japanese Kana Conversion Library-supports converting Japanese Kana to another Japanese character.
- Japanese Text Alignment Library-supports a Japanese-specific Alignment format.
- The Japanese Yomi Auto-Completion Library-Class Library supports the input of the daily input method and an example of a text box control.
- Korean Auto Complete TextBox Control-supports smart sensing of Korean input methods and text box controls automatically completed by input.
- Simplified Chinese Pin-Yin Conversion Library-supports obtaining common attributes of Simplified Chinese characters, such as pinyin, polyphonic words, Homophone Words, and strokes.
- Traditional Chinese to Simplified Chinese Conversion Library and Add-In Tool-supports Conversion between Simplified and Traditional Chinese. This component also contains a plug-in (Add-in) in the integrated development environment of Visual Studio that supports conversion between simplified and Traditional Chinese resource files.
Visual Studio International Feature Pack 2.0 is an extension of version 1.0, including a set of controls and class libraries:
- The Yomigana Framework contains class libraries and controls.
- Class Library: The Yomigana class library allows you to add Yomigana to the string type, and also supports annotation for the general type, any object that implements the IEnumerable interface can be annotated by string type and generic instance. To simplify the comparison of complex annotation strings, a comparison type that supports various Japanese comparison options is designed.
- Some generic classes use generics to enable injection for an enumerable type.
- For special purposes, you can use the generic above to enable certain types of sound injection for a string.
- For special purposes, the StringAnnotation class uses the above generic to enable a string to be voiced, including the parsing and formatting functions.
- A comparator class that compares strings.
- A Data Structure that implements IEnumerable <string>, divides a string into enumerated string segments, and outputs it using IEnumerator <string>.
- Controls:
- The enhanced Ajax/WPF/WinForm text box (TextBox) control is used to capture pronunciation based on user input.
- An enhanced ASP. NET Label control that uses Ruby labels.
- The Chinese Text Alignment Class Library and TextBox Controls contain the WinForm and WPF TextBox Controls that support simplified Chinese Text Alignment, and a Class Library that helps developers easily align and display strings according to Chinese Text.
- Chinese Auto Complete Class Library and TextBox Controls include TextBox Controls that support simplified Chinese and Traditional Chinese Input Methods and automatically Complete WinForm and WPF, and a class library that allows developers to easily add sensor input methods to standard controls and automatically complete functions.
- Korean Auto Complete Class Library and ComboBox Controls include the WinForm and WPF ComboBox Controls that support awareness of Korean input methods and are automatically completed, and a class library that allows developers to easily add sensor input methods to standard controls and automatically complete functions.
- Numeric Formatting Class Library contains classes that support Formatting numbers in five languages into texts. Version 2.0 supports Formatting Arabic numerals into Arabic texts.
It can be seen that this class library is very practical when developing international programs.