22

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.

Primetime
  • 569
  • 4
  • 10
  • 24

6 Answers6

47

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
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
4

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
Brad Falk
  • 196
  • 3
1

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()
Arion
  • 31,011
  • 10
  • 70
  • 88
  • I would rather not move from array of strings to list of strings to list of integers back to array of integers. Was hoping to go from array to array. Will test this out. Thx. – Primetime Mar 08 '12 at 20:39
  • If the intention is to remove items that don't convert properly, then it is more efficient to use a List(Of Integer) then to an array, as you don't know the size of the array without checking if all of them can convert first. – vcsjones Mar 08 '12 at 20:44
  • @Primetime: Works just as well with arrays... You'd just use `ToArray` instead of `ToList`. – Meta-Knight Mar 08 '12 at 20:49
  • @Primetime, you do not need to move from an array of strings to a list of strings. I'd rather not do that either, so i don't. :) This answer is great. My code from Arion's help splits a string into a string array (using Split), then uses the .Where on the string array. It need not be a List(Of String). – Shawn Kovac Nov 05 '13 at 18:35
1

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
NoAlias
  • 9,218
  • 2
  • 27
  • 46
  • 1
    Note: `CINT` will throw an `OverflowException` if the value is numeric but out of the range of an integer, because [`IsNumeric`](http://msdn.microsoft.com/en-us/library/6cd3f6w1%28VS.71%29.aspx) returns also `true` for values that are in decimal range. The same is true if the value contains a `.` since this could be converted to a Double. – Tim Schmelter Mar 08 '12 at 21:37
  • Thus the warning comment at the beginning of the code block, but good explaination. – NoAlias Mar 08 '12 at 21:41
0

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
dbasnett
  • 11,334
  • 2
  • 25
  • 33
0

Everything is much easier ))

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