C # uses Stringhelper to handle Chinese pinyin

Source: Internet
Author: User

Stringhelper String Handling Help

The features that have been implemented now are:

1. Fill the left side of the source string with the given character to reach the specified length

2. Fills the right side of the source string with the given character to reach the specified length

3. function to turn half angle (DBC case)

4. function to turn full angle (SBC case)

5. Chinese pinyin abbreviation

6. Take the phonetic initials of a single character

Wait a minute

Demo: Chinese pinyin abbreviation

? I was written on that Windows Form

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace PyCodeDemo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void label1_Click(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            this.label1.Text = StringHelper.PYConvert(this.textBox1.Text.Trim(), true);        }    }}

Results after implementation:

Here's a look at the Stringhepler code:

Using System;namespace pycodedemo{//<summary>///Stringhelper Summary description. </summary> public class Stringhelper {public Stringhelper () {////T ODO: Add constructor logic here///}//////<summary>///////////////////To fill the left side of the source string with the given character (s) ary>////<param name= "sourcestring" > Source string </param>////<param name= "MaxLength" > string specified length &L      t;/param>////<param name= "Padcharacter" > Pad character </param>////<returns></returns>//  public static string Padstring (string sourcestring, int maxLength, string padcharacter)//public static string          Leftpadstring (string sourcestring, int maxLength, char padcharacter)//{////string result = "";////          if (Sourcestring.length > MaxLength)//{//result = Sourcestring.substring (0,maxlength);// }//else//{//Result = Sourcestring.padleft (maxlength,padcharacter);////while (result).          Length < MaxLength)////{////result + = padcharacter;////}////      }////return result;////}////<summary>////Fill the right side of the source string with the given character to reach the specified length// </summary>////<param name= "sourcestring" > Source string </param>////<param name= "MaxLength "> string specified length </param>////<param name=" Padcharacter "> Pad character </param>////<returns></           returns>//public static string rightpadstring (string sourcestring, int maxLength, char padcharacter)//{//// string result = "";////if (Sourcestring.length > MaxLength)//{//result = so Urcestring.substring (0,maxlength);//}//else//{//result = Sourcestring.padright (maxlength,padcharacter);///while (Result.              Length < MaxLength)///{////result + = padcharacter;//        }////}////Return result;////}/**////<summary> Function of turning half-angle (DBC case)///</summary>//<param name= "Input" > arbitrary string </param>// /<returns> Half-width strings </returns>///<remarks>//full-width spaces are 12288, half-width spaces are 32///other character half-width (33-126) and full-width (                65281-65374) The correspondence is: the difference between 65248///</remarks> public static string Todbc (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);    }///<summary>//Full-width function (SBC case)///</summary>//<param name= "input" > Arbitrary strings </param>//<returns> Full-width strings </returns>///<remarks>//full-width spaces are 12288, half-width empty The correspondence between 32///other character half-width (33-126) and full-width (65281-65374) is: Difference 65248///</remarks> public static Strin G TOSBC (String input) {//half-width to full-width: 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>///pinyin abbreviation///</summary>//<param name= "str" > character string to convert &L t;/param>//<returns> pinyin abbreviation </returns> public static string Pyconvert (string Str,bool Upper) {string Result = string.            Empty; foreach (char c in str) {if ((int) c >= && (int) c <=126) {//Letter and symbol original                Sample retention Result + = c.tostring ();                } else {//cumulative phonetic initials Result + = Convert (C.tostring (), Upper);            }} if (Upper) {result=result.toupper ();        } return Result; }///<summary>///For single character pinyin///</summary>//<param name= "C" > Single kanji to convert &L        t;/param>//<returns> pinyin initials </returns> Internal static string Convert (String C,bool Upper) {string result=string.             Empty;            byte[] array = new BYTE[2];            Array = System.Text.Encoding.Default.GetBytes (c); int i = (short) (array[0]-' plus ') * + (short) (Array[1]-'"));            if (I < 0xb0a1) return "";                          if ((i >= 0xb0a1) && (i <= 0xb0c4)) {result= "a";                          } else if ((I >= 0xb0c5) && (i <= 0xb2c0)) {result= "B";                          } else if ((I >= 0xb2c1) && (i <= 0xb4ed)) {result= "C";                } else if ((I >= 0xb4ee) && (i <= 0xb6e9)) {                          result= "D";                          } else if ((I >= 0xb6ea) && (i <= 0xb7a1)) {result= "E"; } else if ((I >= 0xb7a2) && (i <= 0xb8c0)) {R                          Esult= "F";                          } else if ((I >= 0xb8c1) && (i <= 0xb9fd)) {result= "G";      }      else if ((I >= 0xb9fe) && (i <= 0xbbf6)) {result= "H";                          } else if ((I >= 0xbbf7) && (i <= 0xbfa5)) {result= "J"; } else if ((I >= 0xbfa6) && (i <= 0xc0ab)) {result                          = "K";                          } else if ((I >= 0xc0ac) && (i <= 0xc2e7)) {result= "L";                          } else if ((I >= 0xc2e8) && (i <= 0xc4c2)) {result= "M";                } else if ((I >= 0xc4c3) && (i <= 0xc5b5)) {                          result= "n";                          } else if ((I >= 0xc5b6) && (i <= 0xc5bd)) {result= "O"; } else if ((I >= 0xc5be) && (i <= 0xc6d9))           {result= "P";                          } else if ((I >= 0xc6da) && (i <= 0xc8ba)) {result= "q";                          } else if ((I >= 0xc8bb) && (i <= 0xc8f5)) {result= "R";                } else if ((I >= 0xc8f6) && (i <= 0xcbf9)) {                          Result= "S";                          } else if ((I >= 0xCBFA) && (i <= 0xcdd9)) {result= "T";                          } else if ((I >= 0xCDDA) && (i <= 0xcef3)) {result= "w";                } else if ((I >= 0xcef4) && (i <= 0xd1b8)) {                          result= "X";                       } else if ((I >= 0xd1b9) && (i <= 0xd4d0)) {result= "Y";   } else if ((I >= 0xd4d1) && (i <= 0xd7f9)) {result= "z"; } else {result=string.            Empty; } if (Upper) {Result=result.            ToUpper ();        } return result; public static string Cnametoguidstring (String acname) {Acname=acname.insert (21, "-"). Insert (17, "-"). Insert (13, "-").            Insert (9, "-");            Acname= "{" +acname+ "}";        return acname; } public static string Guidstringtocname (String aguidstring) {Aguidstring=aguidstring.replace ("{ ",""). Replace ("}", "").            Replace ("-", "" ");        return aguidstring; }}}---Cheng original

C # uses Stringhelper to handle 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.