摘要:本文主要向大家介绍了C/C++知识点之C#之使用StringHelper来处理汉字转拼音,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之C#之使用StringHelper来处理汉字转拼音,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
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: 在此处添加构造函数逻辑 // }// /// // /// 用给定的字符填充源字符串的左边以达到指定的长度// /// // /// 源字符串// /// 字符串指定的长度// /// 填充字符// /// // // 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; // // } // /// // /// 用给定的字符填充源字符串的右边以达到指定的长度// /// // /// 源字符串// /// 字符串指定的长度// /// 填充字符// /// // 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;//// } /**//// /// 转半角的函数(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 = 0xB0C5)&&( i = 0xB2C1)&&( i = 0xB4EE)&&( i = 0xB6EA)&&( i = 0xB7A2)&&( i = 0xB8C1)&&( i = 0xB9FE)&&( i = 0xBBF7)&&( i = 0xBFA6)&&( i = 0xC0AC)&&( i = 0xC2E8)&&( i = 0xC4C3)&&( i = 0xC5B6)&&( i = 0xC5BE)&&( i = 0xC6DA)&&( i = 0xC8BB)&&( i = 0xC8F6)&&( i = 0xCBFA)&&( i = 0xCDDA)&&( i = 0xCEF4)&&( i = 0xD1B9)&&( 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/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号