我正在开发 Windows 窗体应用程序。我使用 RichTextBox.LoadFile
方法从文件加载文本并突出显示文本的某些部分。文件中的文本包含返回字符。
假设我要高亮高亮
。首先我找到 startIndex
和 highlight
部分的 length
,然后使用 RichTextBox.Select(startIndex, length)
并给它一些颜色。
当我使用SubString
方法时,我可以找到正确的文本。但是当我将 startIndex
和 length
的相同值应用于 Select
方法时,突出显示的部分变为 [space][space][ space]the highli
.看起来 Select
方法考虑了一些返回字符并导致了一些问题。
我该如何解决?
最佳答案
我认为你应该使用 RichTextBox
的 Find()
方法:
int nextStartIndex;
public void Find(string keyword){
int i = richTextBox1.Find(keyword, nextStartIndex, RichTextBoxFinds.None);
if(i != -1) {
nextStartIndex = i + keyword.Length;
}
}
关于c# - RichTextBox.Select 与 SubString 方法之间的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18248176/