备注:其中的PY,WB为码表;dll找不到上传得地方
using System;
using System.IO;
using System.Collections;
namespace ChsHelper
{
///
/// 如何把汉字转成五笔与拼音(首字母或全部字母)
/// 结合网上资源改写
/// by [email protected]
/// 2006-4-25
///
public class ChsToLetter
{
private ArrayList arrayPY = new ArrayList();
private ArrayList arrayWB = new ArrayList();
public ChsToLetter()
{
init();
}
#region 加载资源
///
/// 加载资源
///
public void init()
{
//py
StreamReader fr = new StreamReader(this.GetType().Assembly.GetManifestResourceStream("ChsHelper.PY"),System.Text.UnicodeEncoding.Default);
string str = string.Empty;
for(int i=0;i <20902;i++)
{
try
{
str = fr.ReadLine();
arrayPY.Add(new ConvertClass(str.Substring(1),str.Substring(0,1)));
}
catch(Exception e)
{
//Console.Write(e.Message);
}
}
fr.Close();
//wb
fr = new StreamReader(this.GetType().Assembly.GetManifestResourceStream("ChsHelper.WB"),System.Text.UnicodeEncoding.Default);
for(int i=0;i <6765;i++)
{
try
{
str = fr.ReadLine();
arrayWB.Add(new ConvertClass(str.Substring(1),str.Substring(0,1)));
}
catch(Exception e)
{
//Console.Write(e.Message);
}
}
fr.Close();
}
#endregion
#region GetPY
///
/// 获得拼音
///
/// 字符串
/// 是否大写
/// 全部字母或首字母
///
public string GetPY(string str,bool isUp,bool full)
{
string tmpStr = string.Empty;
string ssStr = string.Empty;
foreach(char c in str)
{
if((int)c >= 33 && (int)c <=126)
{
//字母和符号原样保留
tmpStr += c.ToString();
}
else
{
//累加拼音声母
ssStr = GetValue(arrayPY,c.ToString(),full);
tmpStr += isUp == true? ssStr.ToUpper(): ssStr;
}
}
return tmpStr;
}
#endregion
#region GetWB
///
/// 获得五笔码
///
/// 字符串
/// 是否大写
/// 全部字母或首字母
///
public string GetWB(string str,bool isUp,bool full)
{
string tmpStr = string.Empty;
string ssStr = string.Empty;
foreach(char c in str)
{
if((int)c >= 33 && (int)c <=126)
{
//字母和符号原样保留
tmpStr += c.ToString();
}
else
{
//累加拼音声母
ssStr = GetValue(arrayWB,c.ToString(),full);
tmpStr += isUp == true? ssStr.ToUpper(): ssStr;
}
}
return tmpStr;
}
#endregion
#region GetValue
///
/// 查找码表
///
/// 数组
/// 查找值
/// 全部字母或首字母
///
public string GetValue(ArrayList list,string Value,bool full)
{
System.Collections.IEnumerator myEnumerator = list.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if(((ConvertClass)myEnumerator.Current).Value == Value)
{
if(full)
return ((ConvertClass)myEnumerator.Current).Key;//全码
else
return ((ConvertClass)myEnumerator.Current).Key.Substring(0,1);//首字母
}
}
return String.Empty;
}
#endregion
}
#region 转换类
///
/// ConvertClass
///
public class ConvertClass
{
private string _key;
private string _value;
public ConvertClass(string key,string Value)
{
this._key = key;
this._value = Value;
}
public String Value
{
get{ return this._value;}
}
public String Key
{
get{ return this._key;}
}
}
#endregion
}