I have this text:
“555”;”general”;”pfss”;”16”
“444”;”compa”;”sss”;”5”
In visual basic 2008 express edition
I want to find in which position are all the ;
The result must be:
5,15,22,32,40,46
I have this text:
“555”;”general”;”pfss”;”16”
“444”;”compa”;”sss”;”5”
In visual basic 2008 express edition
I want to find in which position are all the ;
The result must be:
5,15,22,32,40,46
There is no existing method, but you can write one easily. For example as extension:
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()>
Public Function FindAllIndex(Of T)(ByVal items As IEnumerable(Of T), predicate As Func(Of T, Boolean)) As Int32()
Dim allIndexes As New List(Of Int32)
Dim index As Int32 = 0
For Each item As T In items
If predicate(item) Then
allIndexes.Add(index)
End If
index += 1
Next
Return allIndexes.ToArray()
End Function
End Module
Usage:
Dim allIndexes as Int32() = text.FindAllIndex(Function(c) c = ";"c)
This generic version supports any type and condition. It works under VS2008.
I'd suggest Linq and also to handle the Carriage Return / Linefeed contained in your text - at least if you expect the exact indexes specified by your desired output.
Dim chars = text.Replace(Environment.NewLine, String.Empty).ToCharArray()
Dim positions = Enumerable.Range(0, chars.Length).Where(Function(i) chars(i) = ";"c).ToArray()
Caution
This answer is tailored for the mentioned output values - however it will most likely not help you in what ever task you are doing.
Disclaimer: I translated this solution from diesel's comment on c# Array.FindAllIndexOf which FindAll IndexOf