Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get string that starts at the begining of the line

Status
Not open for further replies.

kruxty

Programmer
Jul 17, 2001
197
PT
well,
i need to get functions (eg: "Private Sub"; Public Sub" or just "Sub"). But if i search just for a string with the words private, public or sub the program gives other strings that are in the middle of the code that contains this words.
So i had think that if i search only the strings that starts at the begining of the line, since the others have tab's, and search for that words i can get what i want.
For now it's all

Tkx,
Kruxty

PS -> If anyone have other solution i will appreciate!
 
Hi,

I assume that you are reading a file that contains vb code from a vb program....

You could read one line at a time and check the begning of each line:
------------------------------------
InFile=freefile
open "YourFile" for Input as #InFile
while not eof(infile)
line input #infile, inline
if lcase$(left$(trim$(inline,7)))="private" or _
lcase$(left$(trim$(inline,6)))="public" or _
lcase$(left$(trim$(inline,3)))="sub" then
'do your coding here
end if
wend
--------------------------------------------------------

Sunaj
 
If the file is in a string with all its data including the CRLFs, you can use Instr.
Code:
'lngfound points to last found
If lngFound = 1 then
    lngInstr = Instr(lngFound,strFile,strSearch)
else 
    lngFound = lngFound + 2
End if
lngInstr = Instr(lngFound,strFile,vbCrlf & strSearch)
if lngInstr = 0 then
....not found
End if
 
Following skeleton code illustrates the use of the FileSystemObject in combination with the Split function to do the job. (Add the FileSystemObject through Project Menu -> References -> Sripting Runtime Library.

Public Sub Main()

Dim strFileName As String
strFileName = App.Path & "\modMain.bas"

Dim objFso As FileSystemObject
Set objFso = New FileSystemObject

Dim objTs As TextStream
Set objTs = objFso_OpenTextFile(strFileName, ForReading)


'Split text into separate lines

Dim astrLines() As String
astrLines = Split(objTs.ReadAll, vbCrLf)

Dim lngIndexLines As Long
For lngIndexLines = LBound(astrLines) To UBound(astrLines)

Dim strTrimmedLine As String
strTrimmedLine = LTrim$(astrLines(lngIndexLines))

Select Case strTrimmedLine

Case vbNullString

'Skip, do nothing: Line is Blank


Case Else

'Split line into separate words

Dim astrWords() As String
astrWords = Split(strTrimmedLine, " ")


'Process the first word for comparison

Select Case astrWords(LBound(astrWords))

Case "Public"
Debug.Print astrLines(lngIndexLines)

Case "Private"
Debug.Print astrLines(lngIndexLines)

Case "Sub"
Debug.Print astrLines(lngIndexLines)

Case Else

End Select

End Select

Next lngIndexLines

End Sub



_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Of course a clean exit should (must) include


Set objTs = Nothing
Set objFso = Nothing


before exiting the module!

Sorry _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top