Compare two strings that looks a like but dont match

p.lerdahl

New member
Joined
Jun 13, 2012
Messages
4
Programming Experience
1-3
Hi, I'm new her. I have a strange problem when compare two strings in VB 2010 express.
I use string.compare(item1, item2) It read the string from a text file to fill a combobox with existing dates.
In the beginning the compare say that item1 ="12/05" and Item2 ="12/05" not match. When get the next date it is a match. The routine check if it allready have the next date. If it is already found it get the next line from the file
If not it store the new date in a string to check against and put in the list.

I also tried different compare metod but the result is the same. It just skip the first date and then all works as planned.
Maybe i just dont see the error :)

Here is the routine.


PrivateSub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim result = New List(OfString)
Using reader = New StreamReader("c:\logs\station.txt")' this is a log file with a bunch of lines registred on date and time and them the log message on each line.
Dim line As String = reader.ReadLine()
Dim take = False
Dim ldate As String = ""
Dim lastdate As String = ""
Dim n As Integer
Do While line IsNot Nothing
If Not line = ""Then
ldate = line.Substring(0, 5)
If lastdate = ""Then lastdate = ldate
End If
If Not ldate.Equals(lastdate) Then
result.Add(ldate)
lastdate = ldate
TextBox1.Text = TextBox1.Text & lastdate.ToString & vbCrLf
If lastdate.Length <= 5 Then ComboBox1.Items.Add(lastdate.ToString)
End If
line = reader.ReadLine()
Loop
End Using
ComboBox1.Items.Add(result)
End Sub
 
Last edited:
It always take an round with empty string and then read the first line. I have seen the values when check it on a Break point.
I actualy have after an empty loop and all the first date is not taken into the list. The file looks something like this

12/05 01:00:34 did something
12/05 01:00:36 got some error
12/05 01:00:38 sendt some data
13/05 00:01:00 did something new
13/05 00:01:01 more work
13/05 00:01:02 lost contact with server

I get the 5 first letters in the string into a new string and then compare it with one old if not this is set at the date.

Tip: your reader.ReadLine is only actually reading a line after the first loop.
 
I have done some changes so it read the first line at the first loop. But still it compares two 12/05 to be not equal. Not before next date. Say i have 6 lines and 3 of them starts with 12/05. It wil not compare before it reach 13/05
Here is a readout on the watch
line "12/05 00:00:04 M Closing for the night!" String
ldate "12/05" String
lastdate "12/05" String

The compare line check if those are not equal. If it is not then it add the new to the list.
 
OK I found the problem. It was that if lastdate is empty i set it equal to the ldate. This made the problem.
Thanks for trying to help!
 
Back
Top