我是这个网站的新手,我不知道我是否提供了足够的信息 - 我会尽力 =)
如果您使用 Notepad++,那么您就会知道我在说什么 -- 当用户将 .exe
加载到 Notepad++ 时,NUL
/\x0
字符替换为 NULL
,它具有黑色背景和白色文本。我尝试将其粘贴到 Visual Studio 中,希望获得相同的输出,但它只是粘贴了一些空格...
有谁知道这是某个组合键还是什么?我想用 NULL
字符代替 \x0
,就像 Notepad++ =)
最佳答案
Notepad++ 是一种富文本编辑器,与您的常规记事本不同。它可以显示所有现代文本编辑器中常见的自定义图形。每当 notepad++ 遇到空字符的 ASCII 代码时读取文件时,它不会显示任何内容,而是将字符串“NULL”添加到 UI,将文本背景颜色设置为黑色,将文本颜色设置为白色,这就是您所看到的。您也可以在富文本编辑器中显示任何自定义样式。
注意:这绝不是一个有效的解决方案。我显然遍历了一个读取字符串 2 次只是为了利用已经存在的方法。这可以在一次通过中手动完成。它只是提示您如何操作。我也仔细编写了代码,但没有运行它,因为我目前没有工具。对于任何错误,我深表歉意,让我知道我会更新它
第 1 步:逐行读取文本文件(行以 '\n' 结尾)并用字符串 "NUL"
替换该行的所有空字符实例> 使用 String.Replace()
。最后将修改后的文本附加到您的 RichTextBox
。
第 2 步:使用 String.IndexOf()
重新遍历您的阅读行,查找每个“NUL”字的起始索引。使用这些索引,您可以从 RichTextBox
中选择文本,然后使用 RichTextBox.SelectionColor
和 RichTextBox.SelectionBackColor
richTextBoxCursor
基本上只是表示 RichTextBox
中每一行的起始索引
StreamReader sr = new StreamReader(@"c:\test.txt" , Encoding.UTF8);
int richTextBoxCursor = 0;
while (!sr.EndOfStream){
richTextBoxCursor = richTextBox.TextLength;
string line = sr.ReadLine();
line = line.Replace(Convert.ToChar(0x0).ToString(), "NUL");
richTextBox.AppendText(line);
i = 0;
while(true){
i = line.IndexOf("NUL", i) ;
if(i == -1) break;
// This specific select function select text start from a certain start index to certain specified character range passed as second parameter
// i is the start index of each found "NUL" word in our read line
// 3 is the character range because "NUL" word has three characters
richTextBox.Select(richTextBoxCursor + i , 3);
richTextBox.SelectionColor = Color.White;
richTextBox.SelectionBackColor = Color.Black;
i++;
}
}
关于c# - 为什么Notepad++ [NULL] 字符不粘贴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58689285/