Results 1 to 4 of 4

Thread: [RESOLVED] Find (partial string) and replace (whole string)

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2000
    Posts
    154

    Resolved [RESOLVED] Find (partial string) and replace (whole string)

    Hey All,

    I have a text file that looks like this...

    0000001001 eeee
    0000001002 aaaa
    0000001003 bbbb
    0000001004 cccc
    0000001005 dddd


    How can I search for just the numbers on the left, but replace the whole
    string when I find it? In other words, the letters on the right will change,
    and I will not know what they are.

    (example) I need to change the line "0000001003 bbbb" to "0000001003 zzzz"
    but the only info I have is the numbers "0000001003".

    Thanks in advance,
    Ron

    I'm using VB5.


    Here is some code I found on here that almost works, except when I use it
    it just puts the z's in front of the b's (0000001003 zzzz bbbb)...

    Code:
    Public Function ReplaceStringInFile(ByVal StrFileName As String, _
                ByVal StrFind As String, ByVal strReplace As String) As String
    
    Dim strTest As String
    
    
    Open StrFileName For Binary As #1
        strTest = Space(LOF(1))
        Get #1, , strTest
    
        strTest = Replace(strTest, StrFind, strReplace)
        Put #1, 1, strTest
    Close #1
    
    ReplaceStringInFile = strTest
    
    End Function
    
    Private Sub Command1_Click()
    
    ReplaceStringInFile "C:\test.txt", "0000001003", "0000001003 zzzz"
    
    End Sub
    And since I'm using VB5, I'm using this Replace function...

    Code:
    Public Function Replace(ByVal sIn As String, ByVal sFind As _
        String, ByVal sReplace As String, Optional nStart As _
         Long = 1, Optional nCount As Long = -1, _
         Optional bCompare As VbCompareMethod = vbBinaryCompare) As _
         String
    
        Dim nC As Long, nPos As Long
        Dim nFindLen As Long, nReplaceLen As Long
    
        nFindLen = Len(sFind)
        nReplaceLen = Len(sReplace)
        
        If (sFind <> "") And (sFind <> sReplace) Then
            nPos = InStr(nStart, sIn, sFind, bCompare)
            Do While nPos
                nC = nC + 1
                sIn = Left(sIn, nPos - 1) & sReplace & _
                 Mid(sIn, nPos + nFindLen)
                If nCount <> -1 And nC >= nCount Then Exit Do
                nPos = InStr(nPos + nReplaceLen, sIn, sFind, _
                  bCompare)
            Loop
        End If
    
        Replace = sIn
    End Function
    Last edited by rdcody; Aug 18th, 2009 at 05:06 PM.

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