Primetime
Primetime

Reputation: 579

Convert string array to int array

I have tried a couple different ways and cannot seem to get the result I want using vb.net.

I have an array of strings. {"55555 ","44444", " "}

I need an array of integers {55555,44444}

This is a wpf page that is sending the array as a parameter to a crystal report.

Any help is appreciated.

Upvotes: 22

Views: 53890

Answers (6)

Kuzja Bxaa
Kuzja Bxaa

Reputation: 1

Everything is much easier ))

Dim NewIntArray = YouStringArray.Select(Function(x) CInt(x)).ToArray

Upvotes: 0

dbasnett
dbasnett

Reputation: 11773

My $.02

    Dim stringList() As String = New String() {"", "123", "456", "789", "a"}
    Dim intList() As Integer

    intList = (From str As String In stringList
               Where Integer.TryParse(str, Nothing)
               Select (Integer.Parse(str))).ToArray

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460038

You can use the List(Of T).ConvertAll method:

Dim stringList = {"123", "456", "789"}.ToList
Dim intList = stringList.ConvertAll(Function(str) Int32.Parse(str))

or with the delegate

Dim intList = stringList.ConvertAll(AddressOf Int32.Parse)

If you only want to use Arrays, you can use the Array.ConvertAll method:

Dim stringArray = {"123", "456", "789"}
Dim intArray = Array.ConvertAll(stringArray, Function(str) Int32.Parse(str))

Oh, i've missed the empty string in your sample data. Then you need to check this:

Dim value As Int32
Dim intArray = (From str In stringArray
               Let isInt = Int32.TryParse(str, value)
               Where isInt
               Select Int32.Parse(str)).ToArray

By the way, here's the same in method syntax, ugly as always in VB.NET:

Dim intArray = Array.ConvertAll(stringArray,
                        Function(str) New With {
                            .IsInt = Int32.TryParse(str, value),
                            .Value = value
                        }).Where(Function(result) result.IsInt).
                Select(Function(result) result.Value).ToArray

Upvotes: 48

NoAlias
NoAlias

Reputation: 9193

Maybe a few more lines of code than the other answers but...

    'Example assumes the numbers you are working with are all Integers.
    Dim arrNumeric() As Integer

    For Each strItemInArray In YourArrayName

        If IsNumeric(strItemInArray) Then

            If arrNumeric Is Nothing Then

                ReDim arrNumeric(0)
                arrNumeric(0) = CInt(strItemInArray)

            Else

                ReDim Preserve arrNumeric(arrNumeric.Length)
                arrNumeric(arrNumeric.Length - 1) = CInt(strItemInArray)

            End If

        End If

    Next

Upvotes: 1

Brad Falk
Brad Falk

Reputation: 196

You can use the Array.ConvertAll method:

    Dim arrStrings() As String = {"55555", "44444"}
    Dim arrIntegers() As Integer = Array.ConvertAll(arrStrings, New Converter(Of String, Integer)(AddressOf ConvertToInteger))


    Public Function ConvertToInteger(ByVal input As String) As Integer
        Dim output As Integer = 0

        Integer.TryParse(input, output)

        Return output
    End Function

Upvotes: 4

Arion
Arion

Reputation: 31239

Maybe something like this:

dim ls as new List(of string)()
ls.Add("55555")
ls.Add("44444")
ls.Add(" ")
Dim temp as integer
Dim ls2 as List(Of integer)=ls.Where(function(x) integer.TryParse(x,temp)).Select(function(x) temp).ToList()

Upvotes: 1

Related Questions