dwarfmine
dwarfmine

Reputation: 15

Find Array index that contains string

I have file with tags and targets, this is example:

TAG1|TARGET1,TARGET2
TAG2|TARGET3,TARGET4

I start by creating String Array using File.ReadAllLines

Dim MAIN As String() = File.ReadAllLines("")

At some point I have one of targets and I need to know what was the tag index (which array line is it), so for example if I have TARGET3 I want to know it's in second line so it's in MAIN(1) and then I can grab TAG = TAG2.

I can't get it working, I tried few methods:

Array.IndexOf(MAIN,"TARGET3")

always returned -1, it worked with full string tho,

Array.IndexOf(MAIN,"TAG2|TARGET3,TARGET4")

returned 1. I tried with Array.FindIndex, was the same.

So my question is: how to get index of partial array item. Thank you for any help.

Upvotes: 1

Views: 2158

Answers (1)

Steve
Steve

Reputation: 216273

You can use Linq to search your array in this way

Dim search = "TARGET3"
Dim line = MAIN.FirstOrDefault(Function(x) x.Contains(search))

This will return directly the line with the matching word

Upvotes: 1

Related Questions