VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Search and replace a string using a bulletproof method. Written in Visual Basic 2.0 for complete co

by James E. Tracy (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Fri 30th January 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Search and replace a string using a bulletproof method. Written in Visual Basic 2.0 for complete compatibility.

Rate Search and replace a string using a bulletproof method. Written in Visual Basic 2.0 for complete co



    Rem +-----------------------------------------------------------------------+
    Rem |    SearchString    =   String to search.                              |
    Rem |    LookFor         =   String to look for within SearchString         |
    Rem |    ReplaceWith     =   What to replace SearchString                   |
    Rem |                                                                       |
    Rem |    Test=SandR("Mary Had a little lamb","a","aa")                      |
    Rem |                would return:                                          |
    Rem |            Maary Haad aa little laamb                                 |
    Rem |                                                                       |
    Rem |   Compatibility: Visual Basic 2.0                                     |
    Rem |                                                                       |
    Rem |       Programmed by: [email protected]                      |
    Rem |                                                                       |
    Rem +-----------------------------------------------------------------------+
    Rem +-----------------------------------+
    Rem |       Declair variables           |
    Rem +-----------------------------------+
    Dim LeftPart As String
    Dim RightPart As String
    Dim Location As Integer
    Dim LeftLocation As Integer
    Dim RightLocation As Integer
    Rem +-------------------------------+
    Rem |       Initilize variables     |
    Rem +-------------------------------+
    LeftPart = ""
    RightPart = ""
    Location = 0
    Rem ==========================================================
    If Len(LookFor) = 0 Then
        LeftPart = SearchString
    Else
        If LookFor = ReplaceWith Then
            LeftPart = SearchString
        Else
            If Len(SearchString) = 0 Then
                LeftPart = SearchString
            Else
                LeftPart = ""
                RightPart = SearchString
                Rem ============================================
                Do
                    Location = InStr(1, RightPart, LookFor, 0)  '   Case INsensitive
                    If Location = 0 Then
                        LeftPart = LeftPart + RightPart
                    Else
                        If Location = 1 Then
                            LeftPart = LeftPart + ReplaceWith
                            RightLocation = Location + Len(LookFor)
                            If RightLocation > Len(RightPart) Then
                                RightPart = ""
                            Else
                                RightPart = Mid(RightPart, RightLocation)
                            End If
                        Else
                            If Location >= 2 Then
                                LeftLocation = Location - 1
                                RightLocation = Location + Len(LookFor)
                                LeftPart = LeftPart + Left(RightPart, LeftLocation) + ReplaceWith
                                If RightLocation > Len(RightPart) Then
                                    RightPart = ""
                                Else
                                    RightPart = Mid(RightPart, RightLocation)
                                End If
                            End If
                        End If
                    End If
                Loop Until Location = 0
            End If
        End If
    End If
    SandR = LeftPart   '   Return string
End Function


Download this snippet    Add to My Saved Code

Search and replace a string using a bulletproof method. Written in Visual Basic 2.0 for complete co Comments

No comments have been posted about Search and replace a string using a bulletproof method. Written in Visual Basic 2.0 for complete co. Why not be the first to post a comment about Search and replace a string using a bulletproof method. Written in Visual Basic 2.0 for complete co.

Post your comment

Subject:
Message:
0/1000 characters