user6096423 user6096423 - 3 months ago 10
Vb.net Question

Parsing through an Array For Next loop Visual Basic

I am stuck here. Spent hours trying many different approaches but nothing is working

I have an array that holds text that looks like this

4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20


The separator is a pica ("|").

What I am trying to do is run through the array and extract the first two columns in separate strings to compare the values, look for the max, and min.

I am trying to end up with a string like this

4456,4466,4446,4436


The code I have tried looks like this

prices = [the array shown above]
For Each i As String In prices
high = (i.Split("|"))(0)
highs = highs & highs1 & ","

MsgBox(highs)
Next


But the result is:

4,4,5,6,,4,4,5,0,,1,7


etc

not sure where I took a wrong turn here




EDIT

I also tried

prices = [the array shown above]
For Each i As String In prices
high = (i.Split("|"))(0)
highs = highs & high & ","

MsgBox(highs)
Next


But that returns blank, no data at all. There is something wrong with the Split syntax. I am using Visual Basic in MS Visual Studio 2015, so maybe I have the wrong syntax.




EDIT 2

I also gave this a shot

Dim str As String
Dim strArr() As String
Dim count1 As Integer
str = prices 'my array
strArr = str.Split("|")

For count1 = 0 To strArr.Length - 1
high = high & strArr(count1) & ","

MsgBox(high)
Next


This is getting closer, but the result is every price instead of just the high




EDIT 3

Here was the solution:

Dim source As String = prices
Dim stringSeparators() As String = {vbCrLf}
Dim result() As String
result = source.Split(stringSeparators,
StringSplitOptions.RemoveEmptyEntries)

'and the key code courtesy of Zaggler ...

Dim fString As String = String.Join(Of String)(", ", result.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))

MsgBox(fString)


My 'prices' were not set as an array. The above code fixed it.

Answer

Let's take your example below...

4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20

 prices = [the array shown above]
                For Each i As String In prices
                    high = (i.Split("|"))(0)
                    highs = highs & highs1 & ","

                    MsgBox(highs)
                Next

The reason you are getting 4,4,5,6,,4,4,5,0,,1,7 is because for each string you are splitting on the | and then taking the first character adding a comma to it.

If you want to get the first column or index whatever you want to call it before the | you need to loop through each string in that array and select out the values...

'this is my test array...
Dim arr As New ArrayList From {"4456|4450|17", "4466|4430|18", "4446|4420|19", "4436|4410|20"}

Now we can use a String.Join function, cast the array for each item as a string and finally select the first item on the split. This will get every item before the | and put them in a string separated with a comma.

 Dim fString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))

If you want the second section select the 1st index as arrays start at 0...

 Dim sString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(1)))

Here is my screenshot of the outputs...

enter image description here