在我的网站我有一个文本框,让用户输入一组数字是这样的:我怎样才能把绳剪断成片,然后将它们存储在阵列
(118,38,137,15,156,14,157,36,152,49,142,57)
我怎么能这些数字存储在像下面?:
[118 38 137 15 156 14 157 36 152 49 142 57]
在我的网站我有一个文本框,让用户输入一组数字是这样的:我怎样才能把绳剪断成片,然后将它们存储在阵列
(118,38,137,15,156,14,157,36,152,49,142,57)
我怎么能这些数字存储在像下面?:
[118 38 137 15 156 14 157 36 152 49 142 57]
阵列使用Split
方法:
yourString = yourString.Substring(1, yourString.Length - 2) ' Trim parentheses.
Dim result As String() = yourString.Split(","c)
的Split
方法有几个重载,取决于目的。我在这里选择了最简单的方式,只需要一个Character
参数,在这种情况下为","c
,逗号。
myArray = "118,38,137,15,156,14,157,36,152,49,142,57".Split (",")
您可以使用正则表达式:
Dim str As string = "(118,38,137,15,156,14,157,36,152,49,142,57)"
Dim matches As MatchCollection = New Regex(@"\d+").Matches(str)
Dim ints As Integer() = New Integer(matches.Count - 1) }
Dim i as Integer
For i = 0 To ints.Length - 1
ints[i] = int.Parse(If(matches.Item(i).Value <> Nothing, _
a.Item(i).Value, "").ToString())
Next