convert saved string of numbers into an array of integers

coder4life

Member
Joined
Sep 25, 2013
Messages
9
Programming Experience
1-3
i have a comma separated string of numbers inside of a var named num_str

the contents of num_str looks like this: "1,2,3,4,5" etc

i am looking for a way to add num_str to an expression to convert the sting of numbers contained therein to an array of integers


i want to make sure i can simply reference 'num_str' to get the numbers from instead of spelling it out like {"1,2,3,4,5"}
 
Are you familiar with the following?
Splitting a string into an array
Parsing a string into an integer
Using a collection
 
ok, i got this far ... my data string, num_str contains a set of numbers separated by a comma. the last part of the string is a blank entry, so i use .Trim to avoid an error

VB.NET:
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

i am stuck with 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 then do calcs, find MAX and MIN, and find averages, etc.

for now i would like to have the array with the integers so i can do something like this:

VB.NET:
dim array1 As array ()
dim max_array as integer

For ...

array1 = ____

Next

max_array = array1.Max

Thanks!
 
Once you've called Split you have a String array. You can then create an Integer array of the same size. Finally, in the For loop, you get the element at the current index in the String array, convert it, then assign it to the element at the current index in the Integer array.

That assumes that each element in the String array does correspond to an element in the Integer array. The fact that you are using an If statement to do some rudimentary validation suggests otherwise. In that case, you should use a List(Of Integer) instead. That way, you can just Add items and the List will grow dynamically. When you're done, you can call ToArray on the List if you specifically need an array, but you can do most things with a List that you can with an array anyway, plus a few extra.

Also, if you're going to do any validation at all, you should probably do it properly. Replace your If condition with a call to Integer.TryParse and get rid of the Convert.ToInt32 call. TryParse will validate and convert in one go, plus it will reject anything that isn't a valid number; not just empty Strings. I'm fairly sure that there's no need to call Trim either, because most conversion methods will just ignore whitespace.
 
This is one way to do it:
    Private Function GetInts(ByVal str As String) As List(Of Integer)
        Dim Output As New List(Of Integer)

        If str.Length > 0I Then
            Dim IntNumb As Integer
            Dim numbStr() As String = str.Split(","c)
            If numbStr.Length > 0I Then
                For Each arrstr As String In numbStr
                    If Integer.TryParse(arrstr, IntNumb) Then Output.Add(IntNumb)
                Next arrstr
            End If
        End If

        Return Output
    End Function
 
Back
Top