Visual Basic Programmer's Resources/Examples:

String Search/Replace:

'  This sample code is presented as is.
'  Although every reasonable effort has been
'  made to insure the correctness of the example
'  below, Idioma Software Inc. makes no warranty
'  of any kind with regard to this program sample
'  either implicitly or explicitly.

'  This program example may be freely distributed for the
'  use of writing computer programs only. Any other use of
'  this material requires written permission from Idioma Software inc.
'  (c) 2000 Idioma Software inc. All rights reserved.
 
' Title:    String Search/Replace
' Platform: Visual Basic - 32 bit
' Author:   Jon Vote
' Contact:  jon@idioma-software.com
' Date:     02/00

Option Explicit

Function strSchRplce(ByVal sString As String, _
         ByVal sSearchFor As String, _
         ByVal sReplaceWith As String) As String

' Search/Replace
' returns result
' Example sString     = "Hoser"
'         sSearchFor = "os"
'         sReplaceWith = "Oz"
' Returns HOzer
'

Dim lnStr As Long
Dim lnRW As Long
Dim lnSF As Long
Dim s As Long
Dim t As Long
Dim sTemp As String

lnStr = Len(sString)
lnRW = Len(sReplaceWith)
lnSF = Len(sSearchFor)

If sSearchFor = "" Then
  strSchRplce = sString
Else
    t = 1
    s = InStr(t, sString, sSearchFor)
    Do While s > 0
      sTemp = Left$(sString, s - 1) & sReplaceWith
      If s < lnStr Then
        sString = sTemp & Mid$(sString, s + lnSF)
        t = s + lnRW
        lnStr = Len(sString)
        s = InStr(t, sString, sSearchFor)
      Else
        sString = sTemp
        s = 0 ' close loop
      End If
    Loop
    
    strSchRplce = sString
    
End If

End Function

 
 
 
 
 

Powered by
Guatemalan Dark Roast