C#之使用StringHelper来处理汉字转拼音
                                            StringHelper字符串处理帮助
站在用户的角度思考问题,与客户深入沟通,找到兴平网站设计与兴平网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖兴平地区。
现在已经实现的功能有:
1.用给定的字符填充源字符串的左边以达到指定的长度
2.用给定的字符填充源字符串的右边以达到指定的长度
3.转半角的函数(DBC case)
4.转全角的函数(SBC case)
5.汉字转拼音缩写
6.取单个字符的拼音声母
等等
演示一下:汉字转拼音缩写
 我是那Windows窗体写的
代码:
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);
        }
    }
}实现后的结果:

下面看一下StringHepler的代码:
using System;
namespace PyCodeDemo
{
    /// 
    /// StringHelper 的摘要说明。
    ///  
    public class StringHelper
    {
        public StringHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
//      /// 
//      /// 用给定的字符填充源字符串的左边以达到指定的长度
//      ///  
//      /// 源字符串
//      /// 字符串指定的长度
//      /// 填充字符
//      /// 
//      /// 用给定的字符填充源字符串的右边以达到指定的长度
//      ///  
//      /// 源字符串
//      /// 字符串指定的长度
//      /// 填充字符
//      /// 
        /// 转半角的函数(DBC case)
        ///  
        /// 任意字符串
        /// 半角字符串 
        ///
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        /// 
        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);
        }
        /// 
        /// 转全角的函数(SBC case)
        ///  
        /// 任意字符串
        /// 全角字符串 
        ///
        ///全角空格为12288,半角空格为32
        ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
        ///         
        public static string ToSBC(string input)
        {
            //半角转全角:
            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);                
        }
        /// 
        /// 汉字转拼音缩写
        ///  
        /// 要转换的汉字字符串
        /// 拼音缩写 
        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;
        }
        /// 
        /// 取单个字符的拼音声母
        ///  
        /// 要转换的单个汉字
        /// 拼音声母 
        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;
        }
    }
}
---程沐喆原创本文名称:C#之使用StringHelper来处理汉字转拼音
分享URL:http://www.cqwzjz.cn/article/ipgphg.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 