?
Solved

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

Posted on 2005-03-09
5
Medium Priority
?
334 Views
Last Modified: 2010-04-23
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))



 
0
Comment
Question by:dressman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13499784
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 13499801
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 13499810
Although, having re-read your question, you haven't actually stated what the problem is !!!
0
 

Author Comment

by:dressman
ID: 13499844
I was having a problem with the instr function.  You saved me alot of time with Indexof.  Thank you very much!!!!
0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 2000 total points
ID: 13499847
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question