Hallo!
Erstelle ein neues Projekt (Windows Forms) und füge folgenden Democode
in das Startformular ein.
Public Class frmTextBetweenDemo
'ab hier einfügen
Dim tbobefore As New TextBox With _
{.Parent = Me, .Width = 60}
Dim tbobehind As New TextBox With _
{.Parent = Me, .Left = 70, .Width = 60}
Dim tbobetween As New TextBox With _
{.Parent = Me, .Left = 140, .Width = 60, _
.ReadOnly = True, .BackColor = Color.LightGray}
Dim WithEvents btnsearch As New Button With _
{.Parent = Me, .Left = 210, .Text = "Suche"}
Dim rtbTextall As New RichTextBox With {.Parent = Me, .Top = 60, _
.Width = 300, .Height = 300}
Private Sub frmTextBetweenDemo_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.Width = 320 : Me.Height = 400
rtbTextall.Text = "Es war einmal ein kleiner Text. " & _
"Er verfügte nur über wenige Worte. " & _
"Irgendwann geriet er in ein RTB-Control. " & _
"Dort wurde er von einer Funktion streng durchsucht. " & _
"Da wurde der kleine Text ganz arg traurig."
End Sub
Private Sub btnsearch_Click(sender As Object, e As System.EventArgs) _
Handles btnsearch.Click
tbobetween.Text = _
GetTextBetween(rtbTextall.Text, tbobefore.Text, tbobehind.Text)
End Sub
Public Function GetTextBetween(ByVal textall As String, _
ByVal textBefore As String, _
ByVal textBehind As String, _
Optional ByVal startindex As Integer = 0) As String
If String.IsNullOrEmpty(Text) Then Return Nothing
If String.IsNullOrEmpty(textBefore) Then Return Nothing
If String.IsNullOrEmpty(textBehind) Then Return Nothing
If startindex < 0 Then startindex = 0
If startindex > textall.Length - textBefore.Length Then Return Nothing
Dim pos1 As Integer = textall.IndexOf(textBefore, startindex)
If pos1 < 0 Then Return Nothing
pos1 += textBefore.Length
Dim pos2 As Integer = textall.IndexOf(textBehind, pos1)
If pos2 < 0 Then Return Nothing
Dim length As Integer = pos2 - pos1
If length = 0 Then Return String.Empty
Return textall.Substring(pos1, length)
End Function
'bis hier einfügen
End Class
Beitrag wurde zuletzt am 25.11.12 um 12:45:40 editiert. |