Click here to Skip to main content
14,640,679 members
Rate this:
Please Sign up or sign in to vote.
See more:
i need help converting a string array into an integer array. i'm reading in my values that are forming the array from a text file (test.txt).
Posted
Comments
Maciej Los 2-Jun-13 17:03pm
   
And where exactly is issue?
Rate this:
Please Sign up or sign in to vote.

Solution 1

What is the problem? For each itme of the array of strings use Int32.Parse[^] (or TryParse[^]) method to convert the string into a number and add the obtained number to the integer array.
You may also skip the 'fill-the-array-of-strings' step and produce directly the array of integers.
   
Comments
   
This is correct, a 5.
However, there is a library generic method to do in in one line of code. Please see my answer.
—SA
CPallini 3-Jun-13 1:46am
   
Thank you.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Essentially, you should create an array of the target type and convert it element by element, in a loop. However, there is a convenient universal generic method in .NET FCL, to do it in one line of code: http://msdn.microsoft.com/en-us/library/exc45z53.aspx[^].

—SA
   
v2
Comments
CPallini 3-Jun-13 1:45am
   
Nice, my 5.
   
Thank you, Carlo.
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

Try This,

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Readtxt As String = "1,2,3,4,5"
        Dim arrTxt() As String = Split(Readtxt, ",")
        Dim arrLength As Integer = arrTxt.Length
        Dim arrNumber(arrLength - 1) As Integer
        For i As Integer = 0 To arrLength - 1
            arrNumber(i) = Integer.Parse(arrTxt(i))
            
        Next
    End Sub
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100