将十进制转换为Char c# [英] Convert Decimal to Char c#

查看:156
本文介绍了将十进制转换为Char c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这个:



I have this:

string str =  "& #1047;& #1076;& #1088;& #1072;& #1074;& #1077;& #1081;, & #1089;& #1074;& #1103;& #1090;"



所以我想要这样的输出:

Здравей,свят"

这是网站:
http://vivenciandoti.blogspot.pt/2001/01/tabela-de- caracteres-especiais-html.html [ ^ ]

以及如何在Winform应用程序中进行转换?
所以希望你能理解我的问题...

问候,
KZ



so I want my output like that:

"Здравей, свят"

Here is the website:
http://vivenciandoti.blogspot.pt/2001/01/tabela-de-caracteres-especiais-html.html[^]

and how I can convert it in my winform app?
So I hope you understand my question...

Regards,
KZ

推荐答案

Regex.Matches方法可用于提取字符的十进制值,然后Select Select 方法可用于转换每个字符十进制值转换为字符,然后可以从chars 数组构造string ,如下所示:

The Regex.Matches method can be used to extract the decimal values of the characters and then Select method of IEnumerable can be used to convert each decimal value into character and then a string can be constructed from the array of chars as shown below:

string str =  "& #1047;& #1076;& #1088;& #1072;& #1074;& #1077;& #1081;, & #1089;& #1074;& #1103;& #1090;";

MatchCollection charNums = Regex.Matches(str,@"(?<=#\s*)\d+(?=\s*)", 
                                    RegexOptions.CultureInvariant);
string strFinal = new string(charNums.OfType<Match>().Select (
                            m => (char)Convert.ToInt32(m.Value)).ToArray());
Console.WriteLine (strFinal);


输出


Output

Здравейсвят


没有用漂亮的整齐的方法包装,但这也有效:

Not wrapped in a nice tidy method, but this also works:

string str = "& #1047;& #1076;& #1088;& #1072;& #1074;& #1077;& #1081;, & #1089;& #1074;& #1103;& #1090;";

 str = Regex.Replace(str, @"& #(\d+);", m => ((char)int.Parse(m.Groups[1].Value)).ToString());



输出:



Output:

Здравей, свят


这篇关于将十进制转换为Char c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆