String variable into integer array

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Hi!

I'm wondering how can I get integer type of array easier of the numbers
in string type variable when numbers are separated with comma ",".

I made it like code below, but obviously there is an easier way to do it.

'// Readed string from ConfigurationSettings
Dim strNums As String = "10,20,30,40"

Dim arrStr As String() = strNums.Split(Char.Parse(","))
Dim arrInt(arrStr.Length - 1) As Integer

For i As Integer = 0 To arrStr.Length - 1
arrInt(i) = CType(arrStr(i), Integer)
Next

....code continues
 
There are slight variations on what you have done, but I dont think there is
a more streamline way code wise to convert arrays of those types.
 
Hi,

I dont see a better way. Just thought you should know that you can
replace Char.Parse(",") with ","c

Ken
 
Mika M said:
I'm wondering how can I get integer type of array easier of the numbers in
string type variable when numbers are separated with comma ",".

I made it like code below, but obviously there is an easier way to do it.

'// Readed string from ConfigurationSettings
Dim strNums As String = "10,20,30,40"

Dim arrStr As String() = strNums.Split(Char.Parse(","))
Dim arrInt(arrStr.Length - 1) As Integer

For i As Integer = 0 To arrStr.Length - 1
arrInt(i) = CType(arrStr(i), Integer)
Next

I think the solution is basically OK. However, some suggestions:

* I would use '","c' instead of 'Char.Parse(",")'. Appending the 'c' to
the
one-character string literal will make it a character literal. No need
for
a conversion at runtime.

* Instead of 'CType(arrStr(i), Integer)' you could write 'CInt(arrStr(i))'
which makes code shorter and IMO more readable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top