Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27350

VS 2012 Tricky String Problem

$
0
0
Hi Guys!
I have a txt file like:
#3 DATE TIME Temp. Humidity
1 08-19-2013 20:58:00 24.6 Date: 08-20-2013
2 08-19-2013 20:58:05 40.8 ID1= 20
3 08-19-2013 20:58:10 24.7 40.7 Sample_Rate= 5sec.
4 08-19-2013 20:58:15 Total_Records= 291
5 08-19-2013 20:58:20 24.7 40.7 Unit= Metric(oC)
6 08-19-2013 20:58:25 24.7 40.7
And I am parsing this log to Excel. I am simply following these steps:
First I am parsing all lines to an array
Code:

Dim lines() As String
        lines = File.ReadAllLines(filename(vi)).Where(Function(s) s.Trim() <> String.Empty).ToArray()

Then I am parsing each word of array to another array and then, to revelant Excel cells
Code:

Dim mystring() As String
For ii As Integer = 1 To (lines.Count - 1)
            mystring = lines(ii).Trim.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
            xlWorkSheet1.Cells(vii, 1) = vii - 1
            xlWorkSheet1.Cells(vii, 2) = mystring(1).Trim & " " & mystring(2).Trim
            xlWorkSheet1.Cells(vii, 3).Value = mystring(3).Trim
            xWorkSheet1.Cells(vii, 4).Value = mystring(4).Trim
vii+=1
Next

vii is another thing. I don't use the logger's row numbering for some reasons. Anyway...
Now, the tricky part comes;

I am using several 'Select Case's to count "mystring" and determine if Humidity data or both Temperature and Humidity data are missing or not. For example, mystring count should return 7 for the first line after headers(second line of the file). 1, 08-19-2013, 20:58:00, 24.6, Date:, 08-20-2013.
6? You see Humidity data is missing!

So, with a Select Case I don't use the following line "xWorkSheet1.Cells(vii, 4).Value = mystring(4).Trim"
Code:

For ii As Integer = 1 To (lines.Count - 1)
            mystring = lines(ii).Trim.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
                    Select Case mystring.Count
                        Case 7
                                    xlWorkSheet1.Cells(vii, 1) = vii - 1
                                    xlWorkSheet1.Cells(vii, 2) = mystring(1).Trim & " " & mystring(2).Trim
                                    xlWorkSheet1.Cells(vii, 3).Value = mystring(3).Trim
                                    xWorkSheet1.Cells(vii, 4).Value = mystring(4).Trim
                        Case 6
                                    xlWorkSheet1.Cells(vii, 1) = vii - 1
                                    xlWorkSheet1.Cells(vii, 2) = mystring(1).Trim & " " & mystring(2).Trim
                                    xlWorkSheet1.Cells(vii, 3).Value = mystring(3).Trim
                        Case 5
                                    xlWorkSheet1.Cells(vii, 1) = vii - 1
                                    xlWorkSheet1.Cells(vii, 2) = mystring(1).Trim & " " & mystring(2).Trim
                      End Select
        Next

Finally, my question is about the next line. On the third line I have only Humidity data.
Especially this problem occurs on 'Case 6'.
If I still use this line "xlWorkSheet1.Cells(vii, 3).Value = mystring(3).Trim", it will continue to parse the Humidity data to the Temperature column of Excel!
Actually it should be ""xlWorkSheet1.Cells(vii, 4).Value = mystring(3).Trim".
What would you suggest to determine if the missing count is because of Humidity or Temperature data?

Thanks a lot!

Viewing all articles
Browse latest Browse all 27350

Trending Articles