ASP. NET core uses Microsoft's official class library for Chinese pinyin

Source: Internet
Author: User

One, NuGet package

Pinyin: Install-package Simplifiedchinesepinyinconversion

Simplified-Traditional Mutual transfer: Install-package Traditionalchinesetosimplifiedconverter

Second, C # code

    classProgram {Static voidMain (string[] args) {            stringName="Liuda"; Console.WriteLine ($"{name}\n full spell: {getpinyin (name)},\n first spell: {getfirstpinyin (name)},\n traditional characters: {gettraditional (name)},\n simplified: {getsimplified (Gettraditional (Name))}");        Console.readkey (); }                /// <summary>         ///converting Chinese characters to pinyin/// </summary>         /// <param name= "str" >Kanji</param>         /// <returns>full Spell</returns>          Public Static stringGetpinyin (stringstr) {            stringR =string.            Empty; foreach(CharObjinchstr) {                Try{Chinesechar Chinesechar=NewChinesechar (obj); stringt = chinesechar.pinyins[0].                    ToString (); R+ = T.substring (0, T.length-1); }                Catch{R+=obj.                ToString (); }            }            returnR; }        /// <summary>         ///convert Chinese characters to pinyin initials/// </summary>         /// <param name= "str" >Kanji</param>         /// <returns>First Letter</returns>          Public Static stringGetfirstpinyin (stringstr) {            stringR =string.            Empty; foreach(CharObjinchstr) {                Try{Chinesechar Chinesechar=NewChinesechar (obj); stringt = chinesechar.pinyins[0].                    ToString (); R+ = T.substring (0,1); }                Catch{R+=obj.                ToString (); }            }            returnR; }        //<summary>        ///convert simplified to traditional/// </summary>         /// <param name= "str" >Simplified Chinese</param>         /// <returns>Traditional</returns>          Public Static stringGettraditional (stringstr) {            stringR =string.            Empty; R=Chineseconverter.convert (str, chineseconversiondirection.simplifiedtotraditional); returnR; }        /// <summary>         ///traditional conversion to simplified/// </summary>         /// <param name= "str" >Traditional</param>         /// <returns>Simplified Chinese</returns>          Public Static stringGetsimplified (stringstr) {            stringR =string.            Empty; R=Chineseconverter.convert (str, chineseconversiondirection.traditionaltosimplified); returnR; }        #endregion    }}
View Code

Run results

ASP. NET core uses Microsoft's official class library for Chinese pinyin

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.