5

我在名为 num_str 的 var 中有一个逗号分隔的数字字符串

num_str 的内容如下所示:“1,2,3,4,5”等

我正在寻找一种将 num_str 添加到表达式以将其中包含的数字字符串转换为整数数组的方法

我想确保我可以简单地引用“num_str”来获取数字,而不是像 {"1,2,3,4,5"}

我试过这个,其中“num_str”包含数字

Dim test As String = Nothing
Dim result() As Integer = Int32.TryParse(num_str.Split(","c))
For i = 0 To result.Length - 1
   test += result(i)
Next

但这不起作用

我正在寻找的是带有数字数组的结果

4

4 回答 4

15
Dim totalValue = str.
                 Split(","c).
                 Select(Function(n) Integer.Parse(n)).
                 Sum()
于 2013-10-01T23:05:16.183 回答
3

Try this to create integer array

Dim numbers = str.Split(","c).[Select](Function(n) Integer.Parse(n)).ToList()

Then you can use the loop you are using at the moment to append value to string

于 2013-10-01T16:54:01.610 回答
0

你可以这样做:

Dim num_str As String = ...
Dim str() As String = num_str.Split(",")
Dim result(str.Length - 1) As Integer
For i = 0 To str.Length - 1
    result(i) = str(i)
Next
于 2013-10-03T08:53:49.483 回答
0

我只是在为一个客户做这个,我发现了一段优雅的小代码,它将替换你的单个字符串值并很容易有效地将它转换为整数(或更准确地说是“双”),并且可以用于“查找和转换”一样容易。我将其中的一堆写到 GPA 计算器中,该计算器将输入的字母等级更改为 GPA 数字格式以进行数学转换和显示,除了这三行和一个小循环之外没有其他任何内容。

For m = 0 To UBound(myArray)
myArray(m) = Replace(myArray(m), "thisstring", 0.0)  'integer 0.0 can be any
Next m
于 2016-01-25T08:59:26.020 回答