110,529
社区成员
发帖
与我相关
我的任务
分享
Regex r=new Regex(@"\&#(?<number>\d+);");
MatchCollection mmm=r.Matches("花都HOLON(会)都");
foreach(Match m in mmm)
{
Console.Write(m.Groups["number"]);
}
strTest = Regex.Replace(strTest, @"&#(?<code>\d+);", delegate(Match m)
{
string coding = Convert.ToString( Convert.ToInt32(m.Groups["code"].Value), 16);
if(coding.Length!=4)
throw new System.Exception("编码格式不正确");
byte[] bytes = new byte[2];
string lowCode = coding.Substring(0, 2);
bytes[0] = System.Convert.ToByte(lowCode, 16);
string highCode = coding.Substring(2, 2);
bytes[1] = System.Convert.ToByte(highCode, 16);
return System.Text.Encoding.Unicode.GetString(bytes);
});
strTest= Regex.Replace(strTest, textBox3.Text, delegate(Match m)
{
string coding = Convert.ToString( Convert.ToInt32(m.Groups["code"].Value), 16);
string characters = "";
if (coding.Length % 4 != 0)
{
throw new System.Exception("编码格式不正确");
}
for (int i = 0; i < coding.Length; i += 4)
{
byte[] bytes = new byte[2];
string lowCode = coding.Substring(i, 2);
bytes[0] = System.Convert.ToByte(lowCode, 16);
string highCode = coding.Substring(i + 2, 2);
bytes[1] = System.Convert.ToByte(highCode, 16);
string character = System.Text.Encoding.Unicode.GetString(bytes);
characters += character;
}
return characters;
});