I have a file in the project called this.txt. This file contains Coordinates of a drawing.
In each line in the file , there are 4 values. Between each value , There is a character separating the values.
For example : 109 n 66 m 110 a 67 b
Here, There is a character between each value.
In my program , I read the file this.txt using StreamReader.
And make a for loop on the number of lines. In this loop , Every line in the text I save it in an array.
I put characters between the values to put the values in variables using Substring Function.
This Is My Code :
Imports System.IO
Public Class Form1
Dim array2(200) As String
Dim theLine1 As String
Dim theLine2 As String
Dim theLine3 As String
Dim theLine4 As String
Private Sub btn_Read_Click(sender As Object, e As EventArgs) Handles btn_Read.Click
Dim sw2 As New StreamReader("this.txt")
Dim lineCount = File.ReadAllLines("this.txt").Length
For t = 0 To lineCount
array2(t) = sw2.ReadLine
theLine1 = array2(t).Substring(0, array2(t).IndexOf("n")).Trim()
theLine2 = array2(t).Substring(array2(t).IndexOf("n") + 1, array2(t).IndexOf("m")).Trim()
theLine3 = array2(t).Substring(array2(t).IndexOf("m") + 1, array2(t).IndexOf("a")).Trim()
theLine4 = array2(t).Substring(array2(t).IndexOf("a") + 1, array2(t).IndexOf("b")).Trim()
Next
End Sub
End Class
The program read the first value corectly and put it in theLine1 Variable. But In next value , The program read the second value and the third value and put it in theLine2 Variable. Using the previous Example : 109 n 66 m 110 a 67 b
The program put 66 m 110 in the theLine2 Variable. And I don't want this and I don't know why this happen. I just want put the next value in theLine2 Variable.
And also when the program reach the theLine3 Varaible. The program show to me an error.
This is the error :
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index and length must refer to a location within the string.
As far as I understand, you want to extract the numbers between the letters, line by line. In your substring function, you should have specified the length as the second parameter, indicating “how much further do I need to look”. However, you still have a letter before the number. It's easier to use
Splitinstead.You also don't need a
Stream Reader. Since you didn't useUsing, you might lock the file, preventing it from being manually opened again.I declared the variables for the numbers as integers and kept them local. If you need them globally, feel free to change it back.
I also added checks to ensure that there is always something in the respective read line.
The lines in the txt file:
Result: