问题描述
我有一个Windows.Forms.TextBox,我想允许用户输入转义序列,然后解释为它们所代表的字符。
I have a Windows.Forms.TextBox that I'd like to allow the user to enter escape sequences that are then interpreted as the character they represent.
例如,如果输入以下内容:
For example, if the following is entered:
Hello\r\nWorld
我希望将其存储为字符数组:
I'd like it to be stored as the character array:
H e l l o \r \n W o r l d
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x0D, 0x0A, 0x57, 0x6F, 0x72, 0x6C, 0x64
到目前为止,我使用以下内容来处理'\r'和'\\\
',但我也喜欢能够处理任何其他转义序列,如 \\\
(ctrl + z)或 \\\
(esc)
Thus far, I'm using the following to handle '\r' and '\n', but I'd also like to be able to handle any other escape sequence, like \u001A
(ctrl+z), or \u001B
(esc)
string str = txtBox.Text.Replace("\\r","\r").Replace("\\n","\n");
...必须有一个更通用的方法来处理这个...
...There has to be a more general way of handling this...
推荐答案
您可以尝试使用静态方法:
You could try to use the System.Text.RegularExpressions.Regex.Unescape
static method:
string str = Regex.Unescape(txtBox.Text);
但不幸的是,它主要适用于正则表达式控制字符。
But unfortunately it applicable mainly for Regex control characters.
这篇关于允许用户在TextBox中输入转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!