1

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.

1
  • @Steve, that's the strange thing - if I make MAIN.Contains("TARGET3") it returns false Commented Sep 1, 2017 at 19:43

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

@dwarfmine, Dim search = "TARGET3" Dim line = MAIN.FirstOrDefault(Function(x) x.Contains(search) that wont compile, did you notice that? Its missing a ). Dim search = "TARGET3" Dim line = MAIN.FirstOrDefault(Function(x) x.Contains(search))...
Welcome, it happens!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.