首页 文章

将字符串数组转换为int数组

提问于
浏览
17

我尝试了几种不同的方法,似乎无法使用vb.net得到我想要的结果 .

我有一个字符串数组 . {“55555”,“44444”,“”}

我需要一个整数数组{55555,44444}

这是一个将数组作为参数发送到晶体报告的wpf页面 .

任何帮助表示赞赏 .

6 回答

  • 4

    也许是这样的:

    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

    您可以使用Array.ConvertAll方法:

    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
    
  • 38

    我的$ .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

    一切都更容易))

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

    您可以使用List(Of T).ConvertAll方法:

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

    或与代表

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

    如果您只想使用Arrays,可以使用Array.ConvertAll method

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

    哦,我错过了样本数据中的空字符串 . 那你需要检查一下:

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

    顺便说一句,这里的方法语法相同,丑陋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
    
  • 1

    也许比其他答案更多的代码行,但......

    '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
    

相关问题