How can I cut the string into pieces and then stored them in array
In my website I have a textbox that allow user to enter a group of numbers like this:
(118,38,137,15,156,14,157,36,152,49,142,57)
How can I store these numbers in an array lik开发者_StackOverflow社区e the following?:
[118 38 137 15 156 14 157 36 152 49 142 57]
Use the Split
method:
yourString = yourString.Substring(1, yourString.Length - 2) ' Trim parentheses.
Dim result As String() = yourString.Split(","c)
The Split
method has several overloads, depending on purpose. I’ve chosen the simplest here, that only takes a single Character
argument, in this case ","c
, a comma.
You can use regular expressions:
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
See String.Split
myArray = "118,38,137,15,156,14,157,36,152,49,142,57".Split (",")
精彩评论