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
{
/// <summary>
/// StringHelper 的摘要说明。
/// </summary>
public class StringHelper
{
public StringHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
// /// <summary>
// /// 用给定的字符填充源字符串的左边以达到指定的长度
// /// </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>
// /// 用给定的字符填充源字符串的右边以达到指定的长度
// /// </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>
/// 转半角的函数(DBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>半角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差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>
/// 转全角的函数(SBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>全角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
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);
}
/// <summary>
/// 汉字转拼音缩写
/// </summary>
/// <param name="str">要转换的汉字字符串</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">要转换的单个汉字</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;
}
}
}
---程沐喆原创
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。