翻译字符串,以自己的实际值 [英] Translate string literals to their actual values

查看:107
本文介绍了翻译字符串,以自己的实际值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:我不想做的问题太长,所以如何从问题的标题翻译成有没有在.NET Framework以这种方法。



只是一个提醒:

  \\\

如果上述是字符串的打印表示,则其内部的版本是一个字符,而不是两个,该代码是0x0A的



不过,我可以写:

 字符串s =\\ \\
;在编辑
//三个字符



这相当于2个字符内部表示 \\ \\ N 。这不是0x0A的性格!



不过,我想实现这一目标。即我已经在字符串的一些数据,我知道是一个字符串的打印表示。我想将其转换为内部表示(东西C#编译器完成所有的时间)。

 字符串可打印=\ \\\
;
串INTERNAL_ =转换(打印);



内部_ 现在将举行只有一个字符, 。代码的0x0A



问:(!)?有准备使用功能,例如转换


< DIV CLASS =h2_lin>解决方案

您可以尝试使用的 System.Text.RegularExpressions.Regex.Unescape 静态方法:

 字符串INTERNAL_ = Regex.Unescape(打印); 



但不幸的是它主要适用于正则表达式控制字符。



测试:

  VAR字符= internal_.ToCharArray(); 



字符阵列与代码1元的0x0A


NOTE: I didn't want to make the question too long, so "how" from the question title translates into "is there a method for this in .Net framework".

Just a reminder:

\n

if the above is printable representation of the string, then its internal version is one character, not two, which code is 0x0a.

However I could write:

string s = "\\n"; // three characters in editor

which translates to internal representation of 2 characters \ and n. This is not 0x0a character!

But I would like to achieve this. I.e. I have already in string some data, which I know is printable representation of a string. And I would like to convert it to internal representation (something C# compiler does all the time).

string printable = "\\n";
string internal_ = convert(printable);

internal_ would hold only one character now, of code 0x0a.

Question: is there ready to use (!) function for such conversion?

解决方案

You could try to use the System.Text.RegularExpressions.Regex.Unescape static method:

string internal_ = Regex.Unescape(printable);

But unfortunately it applicable mainly for Regex control characters.

Test:

var chars = internal_.ToCharArray();

The chars array has 1 element with code 0x0a.

这篇关于翻译字符串,以自己的实际值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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