ol'veyor
ol'veyor

Reputation: 3

VB.net Conditional String Split/Parse using LINQ

I need to parse and separate input strings using VB.NET. Split method works in Example 1, but my problem is when I need to “skip” a space if the following value isn’t in the list like Example2. My code works as expected for String1 using Split, but it fails my needs when it outputs EXT1000 and 3 as individual values - they should be part of the original string if not found in the list. I would prefer to keep it in Linq if possible. Generate some type of collection to be used for later manipulation. The main output will end up with a For Each “Results” WriteLine in text file.

list = {“AB”, “CD”, “EFG”, “HI”, …. “MN”, “OP”, “QR”…}

(results from LINQ from external file. I’m not using it in my code yet, but I think I’ll need to incorporate some comparison)

Upvotes: 0

Views: 1015

Answers (1)

NoAlias
NoAlias

Reputation: 9193

Dim MasterList As String() = {"AB", "CD", "EFG", "HI", "MN", "OP", "QR"}

Dim OtherList As String() = {"AB3", "CD4", "EFG1000", "3"}

Dim DesiredList As String() = (From strValueInMasterList As String In MasterList _
                               From strValueFromOtherList As String In OtherList _
                               Where strValueFromOtherList.StartsWith(strValueInMasterList) _
                               Select strValueFromOtherList).ToArray()

Upvotes: 0

Related Questions