none
String of integers to array of integers

    Question

  • Hi

    How can I convert a comma separated string of integers into an array of integers?

    Thanks

    Regards

    Saturday, March 25, 2017 11:24 PM

Answers

  • Here is one method that if an invalid item in the string array is parsed, zero is returned

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim arrStrings() As String = {"5", "4", "A", "11"}
        Dim arrIntegers() As Integer = Array.ConvertAll(arrStrings,
                New Converter(Of String, Integer)(Function(input)
                                                        Dim output As Integer = 0
    
                                                        Integer.TryParse(input, output)
    
                                                        Return output
                                                    End Function))
    
    
        For Each item As Integer In arrIntegers
            Console.WriteLine(item)
        Next
    
    End Sub
    5
    4
    0
    11



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Y a h y a Sunday, March 26, 2017 10:13 PM
    Sunday, March 26, 2017 12:09 AM
    Moderator
  • How can I convert a comma separated string of integers into an array of integers?

    It's not entirely clear what you are working with - a string with
    "numbers" separated by commas:

    Dim str As String = "123,346, 789, 11 , 999"

    Or a set of numeric strings separated by commas.

    Assuming the first, here's "Yet Another Way" which incorporates some of
    the suggestions already made by others with a few new wrinkles:

    Dim str As String = "123,346, 789,abc, 11 , 999"
    Dim sa As String() = str.Split(","c)
    Dim ia As Integer()
    ReDim ia(sa.Length - 1)
    For n As Integer = 0 To sa.Length - 1
        sa(n) = sa(n).Trim()
        If Not Integer.TryParse(sa(n), ia(n)) Then
            Forms.MessageBox.Show("Invalid numeric string : """ & sa(n) & """ Using zero")
            'ia(n) = 0 ' optional if default not zero
        End If
    Next
    

    - Wayne

    "There are many ways to denude a feline."

    • Marked as answer by Y a h y a Wednesday, March 29, 2017 4:16 AM
    Sunday, March 26, 2017 10:16 PM

All replies

  • Use Split
    • Edited by Castorix31 Saturday, March 25, 2017 11:33 PM
    Saturday, March 25, 2017 11:32 PM
  • Hi

    How can I convert a comma separated string of integers into an array of integers?

    Thanks

    Regards

    Check each one that it can be converted to an integer and if so, add it to a NEW list of integer.

    At the end of that, if the count of the list is greater than zero, use the .ToArray method and you have an array of integers.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, March 25, 2017 11:33 PM
  • Here is one method that if an invalid item in the string array is parsed, zero is returned

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim arrStrings() As String = {"5", "4", "A", "11"}
        Dim arrIntegers() As Integer = Array.ConvertAll(arrStrings,
                New Converter(Of String, Integer)(Function(input)
                                                        Dim output As Integer = 0
    
                                                        Integer.TryParse(input, output)
    
                                                        Return output
                                                    End Function))
    
    
        For Each item As Integer In arrIntegers
            Console.WriteLine(item)
        Next
    
    End Sub
    5
    4
    0
    11



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Y a h y a Sunday, March 26, 2017 10:13 PM
    Sunday, March 26, 2017 12:09 AM
    Moderator
  • A string of integers does not exist.

    One integer can represent more figures but that is not a string, please be more clear what you mean. 

    To set it to string is simply with the overloaded ToString  or CSV


    Success
    Cor


    Sunday, March 26, 2017 8:17 AM
  • How can I convert a comma separated string of integers into an array of integers?

    It's not entirely clear what you are working with - a string with
    "numbers" separated by commas:

    Dim str As String = "123,346, 789, 11 , 999"

    Or a set of numeric strings separated by commas.

    Assuming the first, here's "Yet Another Way" which incorporates some of
    the suggestions already made by others with a few new wrinkles:

    Dim str As String = "123,346, 789,abc, 11 , 999"
    Dim sa As String() = str.Split(","c)
    Dim ia As Integer()
    ReDim ia(sa.Length - 1)
    For n As Integer = 0 To sa.Length - 1
        sa(n) = sa(n).Trim()
        If Not Integer.TryParse(sa(n), ia(n)) Then
            Forms.MessageBox.Show("Invalid numeric string : """ & sa(n) & """ Using zero")
            'ia(n) = 0 ' optional if default not zero
        End If
    Next
    

    - Wayne

    "There are many ways to denude a feline."

    • Marked as answer by Y a h y a Wednesday, March 29, 2017 4:16 AM
    Sunday, March 26, 2017 10:16 PM