您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。