Hallo, ich hab mal wieder eine kurze Frage. Kann man einen String auch suchen, indem man den Index festlegt bei dem der Wert in einer rtb ist?
(z.B. ein 4tes "Hallo" in einer rtb mit insgesamt 10 "Hallo"s) Wenn ja wie legt man das fest? Alleine mit IndexOf und LastIndexOf gehts ja nicht. Hab schon folgendes versucht:
Dann mit Regular Expressions:
mit
Leider funktionierte das nicht richtig. Hat jemand vielleicht andere Vorschläge?
(z.B. ein 4tes "Hallo" in einer rtb mit insgesamt 10 "Hallo"s) Wenn ja wie legt man das fest? Alleine mit IndexOf und LastIndexOf gehts ja nicht. Hab schon folgendes versucht:
VB.NET-Quellcode
- Dim lastFindIndex As Integer = 0
- Dim stringToFind As String = TextBox5.Text
- Dim currentNumberOfMatches = 0
- Dim matchToStopAt As Integer = CInt(TextBox4.Text)
- While lastFindIndex > -1
- currentNumberOfMatches += 1
- If matchToStopAt = currentNumberOfMatches Then
- Exit While
- End If
- lastFindIndex = RichTextBox2.Text.Text.IndexOf(stringToFind, lastFindIndex)
- End While
Dann mit Regular Expressions:
VB.NET-Quellcode
- Public Function SearchForGivenString(text As String, pattern As String, Optional position As Integer = 1) As Match
- Dim found As MatchCollection = Regex.Matches(text, "\b" & pattern & "\b")
- Dim returnMe As Match
- If found.Count = 0 Then
- Return returnMe
- ElseIf position > found.Count Then
- Return found(found.Count - 1)
- Else
- Return found(position - 1)
- End If
- End Function
mit
Leider funktionierte das nicht richtig. Hat jemand vielleicht andere Vorschläge?