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.

0

6 Answers 6

48

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
Sign up to request clarification or add additional context in comments.

6 Comments

I'm not sure if it is implied, but this doesn't cover the case of a failed conversion (empty string).
@vcsjones: You're right, i've missed the empty string in sample data, edited my answer accordingly.
i think it's not ugly when you scratch the ConvertAll delegate: Dim tempInt% : Dim intA As Int32() = intStrA.Where(Function(x) Int32.TryParse(x, tempInt)).Select(Function(x) tempInt).ToArray()
I didn't even realize VB.NET had lambdas before this.
stringList.ConvertAll(AddressOf Int32.Parse) looks the cleaner solution to me
|
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

1 Comment

... you're missing the empty string in the OP's sample data.
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()

4 Comments

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.
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.
@Primetime: Works just as well with arrays... You'd just use ToArray instead of ToList.
@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).
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

2 Comments

Note: CINT will throw an OverflowException if the value is numeric but out of the range of an integer, because IsNumeric 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.
Thus the warning comment at the beginning of the code block, but good explaination.
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

Comments

0

Everything is much easier ))

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

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.