How can I count the number of ControlChars.NewLine in my text file?
Printable View
How can I count the number of ControlChars.NewLine in my text file?
I figured out two way to count the lines in the text file:
Dim lines As String() = IO.File.ReadAllLines("Survey.txt")
'MessageBox.Show("The file had " & lines.Length & " lines.")
And
Dim count As Integer
count = 0
Dim obj As StreamReader
obj = New StreamReader("Survey.txt")
Do Until obj.ReadLine Is Nothing
count = count + 1
Loop
obj.Close()
MessageBox.Show(count)
But how can I check how many records have been entered into the text file? I start a new record and separate them each with a new line (ControlChars.NewLine). Any help?
Is there a way I could do a lop to check for the blank lines?
what exactly you want to accomplish?
you want to know how many records the file has ? what is the format of the file? for each record there is one blank line? like so:
First Record
[blank line]
Second Record
[blank line]
etc ...?
Yes, each record has a blank line separating them and i need to get the number of records. And it's just a text file.
so correct me if i'm wrong, if each record is actually two lines and you have the number of line so this:
gives you the number of records?Code:Dim lines As String() = IO.File.ReadAllLines("Survey.txt")
'MessageBox.Show("The file had " & lines.Length & " lines.")
And
Dim count As Integer
count = 0
Dim obj As StreamReader
obj = New StreamReader("Survey.txt")
Do Until obj.ReadLine Is Nothing
count = count + 1
Loop
obj.Close()
Dim NumberOfLines As Integer = count / 2
MessageBox.Show(NumberOfLines)
Hmmm...I didn't think of it that way...That wold probably work great. Thanks! I will try it and see what I get!
use a For loop and have a counter variable increment by 1 with each line read. at the end of the loop, divide the counter variable by 2 and round down.
remember to use the backslash for the division "\".
lineCount \ 2 = numLineFeeds
@stateofidleness what is the different between using the "/" to "\" when using division ?
It worked! Thanks so much for your help!!!
Glad i could help :)
Please mark thread as resolved.
Division Operator '\, /'
There are two division operations, one of them is a forward slash '/' and the other is a backward slash '\'. The difference between the two is that the forward-slash division operator '/' will result in a floating point (Double) while the backward slash '\' will result in a integer.
Example:
15 / 4 = 3.75
15 \ 4 = 3
Motil's code is great, if the end result is an even number of lines. If there are 5 lines and you divide by 2, your number of line feeds will be 2.5. Not exactly what you're after. The backslash will return an integer (5\2 = 2) which would mean 2 line feeds:
0: Line of text 1
1: linefeed
2: Line of text 2
3: linefeed
4: Line of text 3
5 lines. divide it by 2 with "\" = 2 linefeeds
Thanks for the info about the backslash stateofidlenes that's new to me.
well if the format is always like to OP says it is (Record + Empty line) then he never should have uneven number of lines.
Thanks again for the info.
Wouldn't it be better to get rid of the empty entries since the exact file format is unknown?
Code:Dim validRecords As String() = _
IO.File.ReadAllText("theFile.txt").Split({ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
Dim numRecords As Integer = validRecords.Length