-2

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
Filburt
  • 17,626
  • 12
  • 64
  • 115
  • 1
    Ok and what is the problem? If the answer is *I dont know how to do that*, you need to do some (more) research. This is not a tutorial site – Ňɏssa Pøngjǣrdenlarp Jun 21 '17 at 14:47
  • 1
    I suspect, that by obtaining these positions you want to split fields, another reason comes not to mind. In this case, research `Split` :) –  Jun 21 '17 at 15:16
  • I'd recommend looking at [TextFieldParser](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx) instead of mucking around with indexes of delimiters. – Filburt Jun 21 '17 at 16:04

2 Answers2

1

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

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

Filburt
  • 17,626
  • 12
  • 64
  • 115