הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, April 20, 2016 7:55 PM
I would like to find the second occurrence of string example day=tue below is working code but when the string has more than day= example Dim str As String = "day=mon jim sdasd sads asdasd ws s brown da33 aakk rryy day=tue day=wed day=thr day=fri" it returns dasd thoughts?
working code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = "day=mon day=tue day=wed day=thr day=fri"
Dim index1 As Integer = GetNthIndex(str, "day", 2)
'MsgBox(index1)
Dim index2 As Integer
Dim NewStr As String = str.Substring(index1)
index2 = NewStr.IndexOf(" ")
Dim ss As String = NewStr.Substring(0, index2)
MsgBox(ss)
End Sub
Public Function GetNthIndex(searchString As String, charToFind As Char, n As Integer) As Integer
Dim charIndexPair = searchString.Select(Function(c, i) New With {.Character = c, .Index = i}) _
.Where(Function(x) x.Character = charToFind) _
.ElementAtOrDefault(n - 1)
Return If(charIndexPair IsNot Nothing, charIndexPair.Index, -1)
End Function
Work Smarter Not Harder
All replies (5)
Wednesday, April 20, 2016 8:06 PM ✅Answered
You've declared charToFind as Char but then pass a String value to the method. If you put Option Strict On at the top of your code you would have an error on your call of GetNthIndex().
I think that all you really need is:
Dim str As String = "day=mon day=tue day=wed day=thr day=fri"
Dim index1 As Integer = str.IndexOf("day", str.IndexOf("day") + 1)
-EDIT-
If you still want the GetNthIndex function then it could be implemented as:
Public Function GetNthIndex(searchString As String, subStringToFind As String, n As Integer) As Integer
Dim idx As Integer = searchString.IndexOf(subStringToFind)
Dim count As Integer = 1
While idx > -1 AndAlso count < n
idx = searchString.IndexOf(subStringToFind, idx + 1)
count += 1
End While
Return idx
End Function
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Wednesday, April 20, 2016 9:28 PM ✅Answered
I believe this is used with arrays "searchString.Select".
The below code appears to work but would need error handling probably.
Option Strict On
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
End Sub
Dim str1 As String = "day=mon day=tue day=wed day=thr day=fri"
Dim str2 As String = "day=mon jim sdasd sads asdasd ws s brown da33 aakk rryy day=tue day=wed day=thr day=fri"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox2.Text = GetNthIndex(str2, TextBox1.Text.Split("|"c)(0), CInt(TextBox1.Text.Split("|"c)(1)))
End Sub
Public Function GetNthIndex(StringToSearch As String, SearchString As String, n As Integer) As String
Dim rgx As New Regex("\b" & SearchString & "...." & "\b")
Dim MatchedInfo = rgx.Matches(str1)
If MatchedInfo.Count > 0 Then
Return MatchedInfo(n - 1).ToString
End If
End Function
End Class
La vida loca
Thursday, April 21, 2016 12:54 PM
Thank you, Reed!
Work Smarter Not Harder
Thursday, April 21, 2016 12:55 PM
Thanks Monkeyboy! long time no talk, hope all is well with you.
Work Smarter Not Harder
Thursday, April 21, 2016 5:54 PM
Thanks Monkeyboy! long time no talk, hope all is well with you.
Work Smarter Not Harder
Going pretty good thanks and you too.
La vida loca