我有一个带有3个元素(2,5,6)的数组“string()” 如何将所有元素从字符串转换为int? 我尝试过CInt和Array.ConvertAll但它们没有用。 请告诉我这样做的方法。谢谢。
答案 0 :(得分:1)
你还没有说过你使用Array.ConvertAll
或者显示你的实现有什么类型的问题,但这对我有用。
Module Module1
Sub Main()
Dim mystringArray As String() = New String() {"2", "5", "6"}
Dim myintArray As Integer()
myintArray = Array.ConvertAll(mystringArray, New Converter(Of String, Integer)(AddressOf StringToInteger))
End Sub
Function StringToInteger(st As String) As Integer
Return CInt(st)
End Function
End Module
答案 1 :(得分:0)
您可以使用List(Of T).ConvertAll
Dim stringList = {'2','5','6'}.ToList
Dim intList = stringList.ConvertAll(Function(str) Int32.Parse(str))
答案 2 :(得分:0)
Dim stringList() As String = {"2", "5", "6"}' string array
Dim intList() As Integer = {0, 0, 0, 0, 0}'integer array initialized with 0
For i As Integer = 0 To stringList.Length - 1
intList(i) = CInt(stringList(i))
Next
'Display the list
For i = 0 To intList.Length - 1
MsgBox(intList(i))
Next
答案 3 :(得分:0)
这就像一个魅力:
Dim strArr As New List(Of String)(New String() {"2", "5", "6"})
Dim intList As List(Of Integer) = strArr.ConvertAll(New Converter(Of String, Integer)(AddressOf Integer.Parse))
无需定义自定义解析器。看看它的documentation:
答案 4 :(得分:0)
我的VB生锈但我会做这样的事情:
intList = (From s in stringList Select CInt(s)).ToArray()
答案 5 :(得分:0)
只需使用lambda,
Dim intList As IList(Of Integer)
Dim list1 = "1,2,3".Split(",")
intList = list1.ConvertAll(Function(s) Integer.Parse(s))
或
Dim intList As IList(Of Integer)
Dim list1 = "1,2,3".Split(",")
intList = list1.ConvertAll(AddressOf Integer.Parse)