• Earn real money by being active: Hello Guest, earn real money by simply being active on the forum — post quality content, get reactions, and help the community. Once you reach the minimum credit amount, you’ll be able to withdraw your balance directly. Learn how it works.

Visual Basic Search and replace a string.

Status
Not open for further replies.

Kaway

Banned
User
Joined
Aug 7, 2014
Messages
720
Reputation
0
Reaction score
5,766
Points
243
Credits
0
‎10 Years of Service‎
15%
Please note, if you want to make a deal with this user, that it is blocked.
[HIDE-THANKS]

Code:
>Function SandR (ByVal SearchString As String, ByVal LookFor As String, ByVal ReplaceWith As String) As String
   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
[/HIDE-THANKS]

 
Status
Not open for further replies.
Back
Top