如何转换两个字符@" \\\<br/>"成实际的字符“\\\<br/>”? [英] How to convert a two-character string @"\n" into actual char '\n'?
问题描述
我只是想创造一些解释的脚本语言,而我现在迎接的事情之一是如何解释一个两字符长的字符(与 \
)转化为实际的字符,
I'm just trying to create some interpreter for a script language, and one of the things I meet now is how to interpret a two-chars long char (with \
) into actual char,
例如:事情你无法键入内唯一的单字符:'\\\
为
换行
和'\''
为
等..
For example: things you cannot type within only single char: '\n'
for newline
, and '\''
for '
and so on..
我的翻译得到的信息是这样的字符串:\\\\
因为\
在N
按顺序炭炭后,从一个循环读取文本编辑器中的用户类型。
The information my interpreter gets is such string: "\\n"
because the "\"
is read before the "n"
by order char after char in a loop from the text the user types in the editor.
推荐答案
据我所知你在你的字符串有\\\\
时,要做到这一点最简单的方法是处理它之前,\\\
取代\\\\
。
As I understand you have "\\n" in your string, the easiest way to do this is to replace "\\n" with "\n" before processing it.
string replaced = original.Replace("\\n", "\n");
如果您要更换任何逃脱字符可以使用的 Regex.Unescape 。
If you want to replace any escaped char you can use Regex.Unescape.
要注意的是UNESCAPE将尝试取消转义一切,所以如果你想只取消转义\\。第一序列使用正则表达式来匹配他们(像\\\\ [A-ZA-Z0-9]),然后通过迭代的结果,并与转义版本替换。
Beware that unescape will try to unescape everything, so if you want to unescape only "\\." sequences first use a regex to match them (something like "\\\\[a-zA-Z0-9]"), then iterate through the results and replace with the unescaped version.
这篇关于如何转换两个字符@" \\\<br/>"成实际的字符“\\\<br/>”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!