Results 1 to 7 of 7

Thread: Finding a string in text

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 1999
    Posts
    77

    Arrow Finding a string in text

    The way I've been finding a string in text is a slow way.
    Here's how I would:

    For I = 1 to Len(rtbFile.txt)
    If Mid(rtbFile.txt, I, 9) = "Something" then
    'Found Text
    End if
    Next I

    I know this a slower way than normal. How would I find a string faster than using this method?

    Thank you for any help

    -Justin Gomes
    [email protected]

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    Dim FileName As String
           FileName = "rtbFile.txt"
    
    Dim strTemp As String
    
    Open FileName For Input As #1
      strTemp = Input(LOF(1),1)
    Close #1
    
    If InStr(1,strTemp,"Something") <> 0 Then
      MsgBox "String Found!"
    End If
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Matthew Gates
    Guest
    Use the Like operator.


    Code:
    If rtbFile.txt Like "*Something*" Then Msgbox "Something found!"

  4. #4
    Junior Member
    Join Date
    May 2001
    Posts
    18
    'match case
    If InStr(1,strTemp,"Something",vbBinaryCompare) <> 0 Then
    MsgBox "String Found!"
    End If

    'no match case
    If InStr(1,strTemp,"Something",vbTextCompare) <> 0 Then
    MsgBox "String Found!"
    End If

    for a good tool for programmers.
    please visit: http://www.iCodeRepository.com

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 1999
    Posts
    77

    I'm sorry

    I am sorry I wasn't detailed enough.

    After finding the string I need to know where the string is inside the file.

    For example, the len of the string location

  6. #6
    Matthew Gates
    Guest
    Code:
    Dim iPos As Integer
    Dim FileName As String
    FileName = "rtbFile.txt"
    
    Dim strTemp As String
    
    Open FileName For Input As #1
      strTemp = Input(LOF(1),1)
    Close #1
    
    iPos = InStr(1,strTemp,"Something")
    If iPos <> 0 Then Msgbox "Something found at position: " & iPos

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'open a file and find the first occurance of a string and return the 
    'occurance and the remainder of the file 
    
    'remove option compare text if you want the seach to be case aware 
    
    Option Explicit 
    Option Compare Text 
    
    'contents of file 
    'I was drinking tea in the beverage room of the house 
    
    Private Sub Command1_Click() 
    
    Dim sfile As String, intNum As Integer, myString As String 
    intNum = FreeFile 
    sfile = "C:\my Documents\myfile.txt" 
    
    Open sfile For Input As intNum 
    myString = StrConv(InputB(LOF(intNum), intNum), vbUnicode) 
    Close intNum 
    
    Dim myFind As String, myLen As String, iCount As Integer 
    Dim mySearch As String 
    
    mySearch = "the" 
    For iCount = 1 To Len(myString) 
    myLen = InStr(1, myString, mySearch) 
    Next 
    'the balance of the file including "the first" [the] 
    'in this case [the beverage room of the house 
    myString = Right(myString, Len(myString) - (CInt(myLen) - 1)) 
    MsgBox myString 'result of search 
    
    End Sub
    [/code]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width