C#之使用StringHelper來處理漢字轉(zhuǎn)拼音

StringHelper字符串處理幫助

站在用戶的角度思考問題,與客戶深入溝通,找到興平網(wǎng)站設(shè)計與興平網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站設(shè)計、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋興平地區(qū)。

現(xiàn)在已經(jīng)實現(xiàn)的功能有:

1.用給定的字符填充源字符串的左邊以達(dá)到指定的長度

2.用給定的字符填充源字符串的右邊以達(dá)到指定的長度

3.轉(zhuǎn)半角的函數(shù)(DBC case)

4.轉(zhuǎn)全角的函數(shù)(SBC case)

5.漢字轉(zhuǎn)拼音縮寫

6.取單個字符的拼音聲母

等等

演示一下:漢字轉(zhuǎn)拼音縮寫

?我是那Windows窗體寫的
C#之使用StringHelper來處理漢字轉(zhuǎn)拼音

代碼:

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);
        }
    }
}

實現(xiàn)后的結(jié)果:

C#之使用StringHelper來處理漢字轉(zhuǎn)拼音

下面看一下StringHepler的代碼:


using System;

namespace PyCodeDemo
{
    /// <summary>
    /// StringHelper 的摘要說明。
    /// </summary>
    public class StringHelper
    {
        public StringHelper()
        {
            //
            // TODO: 在此處添加構(gòu)造函數(shù)邏輯
            //
        }
//      /// <summary>
//      /// 用給定的字符填充源字符串的左邊以達(dá)到指定的長度
//      /// </summary>
//      /// <param name="sourceString">源字符串</param>
//      /// <param name="maxLength">字符串指定的長度</param>
//      /// <param name="padCharacter">填充字符</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>
//      /// 用給定的字符填充源字符串的右邊以達(dá)到指定的長度
//      /// </summary>
//      /// <param name="sourceString">源字符串</param>
//      /// <param name="maxLength">字符串指定的長度</param>
//      /// <param name="padCharacter">填充字符</param>
//      /// <returns></returns>
//      public static string RightPadString(string sourceString, int maxLength, char padCharacter)
//      {
//
//          string result = "";
//
//          if (sourceString.Length > maxLength)
//          {
//              result = sourceString.Substring(0,maxLength);
//          }
//          else
//          {
//              result = sourceString.PadRight(maxLength,padCharacter);
//              //              while (result.Length < maxLength)
//              //              {
//              //                  result += padCharacter;
//              //              }
//
//          }
//           
//          return result;
//
//      }

        /**//// <summary>
        /// 轉(zhuǎn)半角的函數(shù)(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        ///<remarks>
        ///全角空格為12288,半角空格為32
        ///其他字符半角(33-126)與全角(65281-65374)的對應(yīng)關(guān)系是:均相差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>
        /// 轉(zhuǎn)全角的函數(shù)(SBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>全角字符串</returns>
        ///<remarks>
        ///全角空格為12288,半角空格為32
        ///其他字符半角(33-126)與全角(65281-65374)的對應(yīng)關(guān)系是:均相差65248
        ///</remarks>        
        public static string ToSBC(string input)
        {
            //半角轉(zhuǎn)全角:
            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>

        /// 漢字轉(zhuǎn)拼音縮寫

        /// </summary>

        /// <param name="str">要轉(zhuǎn)換的漢字字符串</param>

        /// <returns>拼音縮寫</returns>

        public static string PYConvert(string str,bool Upper)

        {

            string Result = string.Empty;

            foreach(char c in str)

            {

                if((int)c >= 33 && (int)c <=126)

                {//字母和符號原樣保留

                    Result += c.ToString();

                }

                else

                {//累加拼音聲母

                    Result += Convert(c.ToString(),Upper);

                }

            }
            if (Upper)
            {
               Result=Result.ToUpper();

            }

            return Result;

        }

        /// <summary>

        /// 取單個字符的拼音聲母

        /// </summary>

        /// <param name="c">要轉(zhuǎn)換的單個漢字</param>

        /// <returns>拼音聲母</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] - '\0') * 256 + ((short)(array[1] - '\0'));

            //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) )
            {
                result="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;
        }

    }
}

---程沐喆原創(chuàng)

當(dāng)前文章:C#之使用StringHelper來處理漢字轉(zhuǎn)拼音
本文網(wǎng)址:http://muchs.cn/article46/ipgphg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化Google、企業(yè)建站、手機(jī)網(wǎng)站建設(shè)、搜索引擎優(yōu)化、營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)