Click here to Skip to main content
15,309,875 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
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?

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.
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
Try This,

VB
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900