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;
}
//另种方法转换
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)