vb.net 将字符串数组转换为 int 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9624355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:59:48  来源:igfitidea点击:

Convert string array to int array

vb.net

提问by Primetime

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

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

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

我有一个字符串数组。{"55555","44444",""}

I need an array of integers {55555,44444}

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

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

这是一个 wpf 页面,它将数组作为参数发送到水晶报告。

Any help is appreciated.

任何帮助表示赞赏。

回答by Tim Schmelter

You can use the List(Of T).ConvertAllmethod:

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

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:

如果您只想使用数组,则可以使用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:

顺便说一下,这里的方法语法是一样的,在 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

回答by Brad Falk

You can use the Array.ConvertAll method:

您可以使用 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

回答by N0Alias

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

回答by Arion

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()

回答by dbasnett

My $.02

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

回答by Kuzja Bxaa

Everything is much easier ))

一切都容易得多))

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