Use Instr to find ***** or ------- in a string

I am searching a text document line by line looking for lines with ****** and ------.  Such as the following lines:
-----------        ------  ----   -----    -------!   -----    -------!   -----    -------!   -----    -------!   -----    -------!
 ******************************************************************************
Here is some code:

 Dim file As New System.IO.StreamReader(sinputfile)
        Dim oneLine As String
        oneLine = file.ReadLine()
        iMaxCnt = 0
        While (oneLine <> "")
            oneLine = file.ReadLine()

            If InStr(oneLine, "---") Or InStr(oneLine, "***") Then
                iMaxCnt += 1
            ElseIf InStr(oneLine, "GRAND TOTAL") Then
                Exit While
            End If
        End While

        file.Close()


I have also tried:
InStr(myData, Chr(42) & Chr(42) & Chr(42))



 
dressmanAsked:
Who is Participating?
 
S-TwilleyConnect With a Mentor Commented:
Also... if your file contains a blank line in the middle... I think your loop may exit prematurely:

Try this (modified from the help file)
==============

 Dim oneLine As String
 iMaxCnt = 0
 
  Dim SReadLine As Stream
  SReadLine = File.OpenRead(sinputfile)

  Dim file As StreamReader = New StreamReader(SReadLine, System.Text.Encoding.ASCII)
  file.BaseStream.Seek(0, SeekOrigin.Begin)

  While (file.Peek() > -1)
            oneLine = file .ReadLine()

            If oneLine.IndexOf("---") >= 0 Or oneLine.IndexOf("***") >= 0 Then
                iMaxCnt += 1
            ElseIf InStr(oneLine, "GRAND TOTAL") Then
                Exit While
            End If
  End While

  file.Close()

=========

I think that is correct... apologies if i modified it slightly wrong
0
 
S-TwilleyCommented:
If you want to test the presence of one string within another... use the following:


If MasterString.IndexOf(SmallerString) >= 0 Then
     'SmallerString is present in MasterString
Else
      ' SmallerString is not present
End If


So in your case:

            If oneLine.IndexOf("---") >= 0 Or oneLine.IndexOf("***") >= 0 Then
                iMaxCnt += 1
            ElseIf InStr(oneLine, "GRAND TOTAL") Then
                Exit While
            End If

===========================

However... in saying that, there might be a more efficient way of counting the instances of those character patterns over an entire file... but I'll have to test that before hand :P
0
 
Carl TawnSystems and Integration DeveloperCommented:
You need to move the second ReadLine() to the end of the loop, you are currently reading two lines before you actually start processing anything. So your code becomes:

        Dim file As New System.IO.StreamReader(sinputfile)
        Dim oneLine As String
        oneLine = file.ReadLine()
        iMaxCnt = 0
        While (oneLine <> "")
            If InStr(oneLine, "---") Or InStr(oneLine, "***") Then
                iMaxCnt += 1
            ElseIf InStr(oneLine, "GRAND TOTAL") Then
                Exit While
            End If
            oneLine = file.ReadLine()
        End While

        file.Close()


Hope this helps.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Although, having re-read your question, you haven't actually stated what the problem is !!!
0
 
dressmanAuthor Commented:
I was having a problem with the instr function.  You saved me alot of time with Indexof.  Thank you very much!!!!
0
All Courses

From novice to tech pro — start learning today.