0

i got this far ... my data string, "num_str" contains a set of ~10 numbers, each separated by a comma. the last part of the string is a blank entry, so i use '.Trim' to avoid an error

Dim i As Integer
        Dim m_data() As String
        m_data = num_str.Split(",")

        For i = 0 To UBound(m_data)
            If m_data(i).Trim.Length > 0 Then
                MsgBox(Convert.ToInt32(m_data(i).Trim))
            End If
        Next i

as you can see from the Msgbox, each of the numbers successfully pass through the loop.

where i am stuck is how to place all of the 'Convert.ToInt32(m_data(i).Trim)' numbers, which are now presumably integers, into an array.

how do i build an array of integers inside the For / Next loop so i can find MAX and MIN and LAST

TIA

2 Answers 2

1

You just need to initialize the array with the zero-based indexer. You can deduce it's initial size from the size of the string():

Dim m_data = num_str.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim intArray(m_data.Length) As Int32

For i = 0 To m_data.Length - 1
    intArray(i) = Int32.Parse(m_data(i).Trim())
Next i

Note that i've also used the overload of String.Split which removes empty strings.

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

Comments

0

This way is more concise using the Select LINQ Operator.

Dim arrayOfInts = num_str.
                Split({","c}, 
                StringSplitOptions.RemoveEmptyEntries).
                Select(Function(v) Int32.Parse(v.Trim()))

Dim minInt = arrayOfInts.Min()
Dim maxint = arrayOfInts.Max()

Comments

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.