Resolved split a textfile

andrews

Well-known member
Joined
Nov 22, 2011
Messages
167
Programming Experience
5-10
Dear moderator,
I have the following problem.
I want to split an existing text file consisting of 1 line: "87,65,28,63,27,56,8,329" into an array myarray of strings like myarray(0) = "87",myarray(1) = "65", myarray(2) = "28" ....
The split separator is dim ch = cchar(",")
I can't find a solution
Thanks for any response
Regards
Andrews
 
You haven't looked very well if you can't find a solution. Read the file into a String and then use String.Split on that, like you would for any other String.

BTW, you don't need to convert a String literal to a Char when you can just use a Char literal:
VB.NET:
Dim ch = ","c
 
Or even just one line:
VB.NET:
Dim data = File.ReadAllText(filePath).Split(","c)
That will give you a String array. If you need actual numbers, you can incorporate that too:
VB.NET:
Dim data = File.ReadAllText(filePath).Split(","c).Select(Function(s) Convert.ToInt32(s)).ToArray()
 
Sorry, moderator, now I get a new error on the code line for some existing files:

Dim text As String = File.ReadAllLines("C:\users\AndreAugust\source\repos\balto-zimmermann\allegridtextbestanden\" & "result.txt")(0)
The error is : System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

At the start I gave number for starting with an existing file, starting with 3 and ending with 27.
The choosen existing file is larger if the number is larger.
For the starting file all works fine for the numbers 3 to 9, but starting with 10 I get the error.
If number is 3 the file result.txt contains 19 items
For 9 there are 217 items
For 10 there it is with 271 items , now the error
And so on, for 27 there are 2107 items
I do not know the reason
By the way, what means (0) at the end of de code line?
Regards
 
Why would you be calling ReadAllLines in the first place? You specifically said that the file contains one line, I told you to read the file int "a String" and I showed how to do that in post #4. I don't know what ReadAllLines has to do with this topic and I don't know what the rest of post #6 means.
 
Back
Top