Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert saved string of numbers into an array of integers

Tags:

arrays

vb.net

i have a comma separated string of numbers inside of a var named num_str

the contents of num_str looks like this: "1,2,3,4,5" etc

i am looking for a way to add num_str to an expression to convert the sting of numbers contained therein to an array of integers

i want to make sure i can simply reference 'num_str' to get the numbers from instead of spelling it out like {"1,2,3,4,5"}

i have tried this, where 'num_str' contains the numbers

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

but that doesn't work

what i am looking for is a result with an array of numbers

like image 211
user2835653 Avatar asked Oct 01 '13 16:10

user2835653


2 Answers

Dim totalValue = str.
                 Split(","c).
                 Select(Function(n) Integer.Parse(n)).
                 Sum()
like image 134
jcwrequests Avatar answered Oct 12 '22 11:10

jcwrequests


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

like image 45
huMpty duMpty Avatar answered Oct 12 '22 09:10

huMpty duMpty