Hi is there any function that would return the indexOf all apples in this string :
string = orange apple apple orange grape grape apple orange
Printable View
Hi is there any function that would return the indexOf all apples in this string :
string = orange apple apple orange grape grape apple orange
I believe you need to writh a function to loop through the string and get the indexes.
I dont know how, that's why i need help..
It would be relatively inefficient but Regex.Matches would do the job in one go. I'd be inclined to write a method call IndexOf repeatedly and return the results in a single array or collection and then call that method as needed.
this is very simple stuf so you should do it your self.
VB Code:
Private Function GetIndexs(ByVal str As String, ByVal exp As String) As Integer() Dim indexes As New List(Of Integer) Dim helper, index As Integer While str <> "" AndAlso helper <> -1 helper = str.Substring(index).IndexOf(exp) If helper <> -1 Then index = helper + index indexes.Add(index) If index + 1 < str.Length - 1 Then index += 1 End If End If End While Return indexes.ToArray End Function
IndexOf is overloaded:VB Code:
Private Function IndexesOf(ByVal str As String, ByVal substr As String) As Integer() Dim indexes As New List(Of Integer) Dim index As Integer = 0 While index < str.Length index = str.IndexOf(substr, index) If index = -1 Then Exit While End If indexes.Add(index) index += 1 End While Return indexes.ToArray() End Function
Yes that defiantly looks cleaner. :)
this is great, thank you very much :wave: :wave: :) :) :D