Convert string array to int array

后端 未结 6 1469
名媛妹妹
名媛妹妹 2021-02-04 03:42

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

相关标签:
6条回答
  • 2021-02-04 03:54

    Everything is much easier ))

    Dim NewIntArray = YouStringArray.Select(Function(x) CInt(x)).ToArray
    
    0 讨论(0)
  • 2021-02-04 03:58

    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
    
    0 讨论(0)
  • 2021-02-04 03:59

    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()
    
    0 讨论(0)
  • 2021-02-04 04:02

    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
    
    0 讨论(0)
  • 2021-02-04 04:06

    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
    
    0 讨论(0)
  • 2021-02-04 04:12

    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
    
    0 讨论(0)
提交回复
热议问题