c# 得到汉字首字母/转换字符
private void getFirstCode()
{
Num = 0;
conn = "Provider=SQLNCLI;Data Source=192.168.0.111,1433;Database=MIS;UID=sa;PWD=;";
string WordBegin = "";
string TermName = "";
sql = "select Term_Name from terms where word_begin is null";
OleDbDataReader odr = db.GetList(conn, sql);
while (odr.Read())
{
Num++;
txt_num.Text = Num.ToString();
TermName = odr.GetValue(0).ToString();
WordBegin = GetChineseSpell(TermName).Substring(0, 1);
sql = "update terms set Word_Begin='" + modsql(WordBegin) + "' where Term_Name='" + modsql(TermName) + "'";
db.ExeSql(conn, sql);
}
MessageBox.Show("获取完成!");
}
static public string GetChineseSpell(string strText)
{
int len = strText.Length;
string myStr = "";
for(int i=0;i<len;i++)
{
myStr += getSpell(strText.Substring(i,1));
}
return myStr;
}
static public string getSpell(string cnChar)
{
byte[] arrCN = Encoding.Default.GetBytes(cnChar);
if(arrCN.Length > 1)
{
int area = (short)arrCN[0];
int pos = (short)arrCN[1];
int code = (area<<8) + pos;
int[] areacode = {45217,45253,45761,46318,46826,47010,47297,47614,48119,48119,49062,49324,49896,50371,50614,50622,50906,51387,51446,52218,52698,52698,52698,52980,53689,54481};
for(int i=0;i<26;i++)
{
int max = 55290;
if(i != 25) max = areacode[i+1];
if(areacode[i]<=code && code<max)
{
return Encoding.Default.GetString(new byte[]{(byte)(65+i)});
}
}
return "*";
}
else return cnChar;
}
//转换字符
private string UnicodeToGB(string content)
{
Regex objRegex = new Regex("&#(?<UnicodeCode>[\\d]{5});", RegexOptions.IgnoreCase);
Match objMatch = objRegex.Match(content);
StringBuilder sb = new StringBuilder(content);
while (objMatch.Success)
{
string code = Convert.ToString(Convert.ToInt32(objMatch.Result("${UnicodeCode}")), 16);
byte[] array = new byte[2];
array[0] = (byte)Convert.ToInt32(code.Substring(2), 16);
array[1] = (byte)Convert.ToInt32(code.Substring(0, 2), 16);
sb.Replace(objMatch.Value, Encoding.Unicode.GetString(array));
objMatch = objMatch.NextMatch();
}
return sb.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(UnicodeToGB("\u8bf7\u6c42\u6709\u6b63\u4e49\u611f\u7684\u597d\u5fc3\u4eba\u5e2e\u5e2e\u6211."));
}
//此方法转换Unicode到汉字
private string UnicodeToCN(string content)
{
string str = content.Replace(@"\u","");
string temp = string.Empty;
for (int i = 0; i < str.Length / 4; i++)
{
temp += (char)Convert.ToInt32(str.Substring(i * 4, 4), 16);
}
return temp;
}
//另种方法转换
2
3/// <summary>
4/// 将Unicode转找为Character
5/// </summary>
6/// <param name="str">原字符串</param>
7/// <returns></returns>
8private string Unicode2Character(string str)
9{
10 string text = str;
11 string strPattern = "(?<code>\\\\u[A-F0-9]{4})";
12 do
13 {
14 Match m = Regex.Match(text, strPattern, RegexOptions.IgnoreCase);
15 if (m.Success)
16 {
17 string strValue = m.Groups["code"].Value;
18 int i = System.Int32.Parse(strValue.Substring(2, 4), System.Globalization.NumberStyles.HexNumber);
19 char ch = Convert.ToChar(i);
20 text = text.Replace(strValue, ch.ToString());
21 }
22 else
23 {
24 break;
25 }
26 }
27 while(true);
28
29 return text;
30}