根据字符串位置复制RichTextBox字符串

时间:2014-04-21 23:41:14

标签: vb.net

我有一个很长的TXT字符串导入RichTextBox。我正在尝试使用基于文本字符串的起点和终点复制该文本的一部分。这是我到目前为止所拥有的,

    Dim StartsearchString As String = "MyStartString"
    Dim EndSearchString As String = "MyEndString"
    Dim Length As Integer
    Dim StartPoint As Integer
    Dim EndPoint As Integer
    Length1 = Len(StartsearchString)
    Length2 = Len(EndSearchString)
    StartPoint = Form2.RichTextBox2.Find(StartsearchString) + Length1
    EndPoint = Form2.RichTextBox2.Find(EndSearchString) - Length2

如何在起点和终点之间复制文本并将其粘贴到新的RichTextBox中?

谢谢。

1 个答案:

答案 0 :(得分:1)

您的EndPoint已关闭,因为您不必减去EndString的长度。也就是说,Find方法正在寻找该索引的开头。

我使用SubString函数以这种方式重新编写它:

Dim StartsearchString As String = "MyStartString"
Dim EndSearchString As String = "MyEndString"
Dim StartPoint As Integer = RichTextBox2.Find(StartsearchString)
Dim EndPoint As Integer = RichTextBox2.Find(EndSearchString)
If EndPoint > StartPoint Then
  OtherRTB.Text = RichTextBox2.Text.Substring(
                     StartPoint + StartsearchString.Length,
                     EndPoint - StartPoint - StartsearchString.Length)
End If