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

标签 vb.net

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

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

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

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

感谢任何帮助。

最佳答案

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

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

关于vb.net - 将字符串数组转换为 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9624355/

相关文章:

vb.net - 为RDLC自定义代码添加多个功能

vb.net - 相当于 "Dim As String * 1"VB6 到 VB.NET

vb.net - 数据访问层是静态的还是基于实例的?

c# - 使用 SharpZipLib 压缩流在 C# 和 VB.NET 中有不同的行为

asp.net-mvc - asp.net MVC 4 中的第二个 MapRoute 无法使用参数

vb.net - 在 Access 中创建关系

asp.net <form> 标记从 asp 中剥离 :literal

c# - 资源 (.resx) 文件有什么好处?

c# - Excel范围使用问题(单元格错误检查)

c# - RIA 服务 + 实体 - 客户端中的日期提前 24 小时