Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace any Character in a String with Another (or none)

Status
Not open for further replies.
Feb 23, 2004
71
US
re archived thread705-1042294
Added a test for Null values.


Public Function ReplaceinString(strSearch As Variant,
strFind As String, strReplace As String) As Variant

'Description - Finds and replaces a string in a string by another string
'Author - Neil Gummow
' Modified - Mike Adel 11/22/2005 - add a test for Null
'System - Access
' Example - replaceinstring("300-06", "-", "") = 30006

Dim intCounter As Integer


If IsNull(strSearch) Then
ReplaceinString = ""
Else
For intCounter = 1 To Len(strSearch)
If Mid(strSearch, intCounter, Len(strFind)) = strFind Then
strSearch = Left(strSearch, intCounter - 1) & strReplace & Mid(strSearch, intCounter + Len(strFind))
End If
Next intCounter
ReplaceinString = strSearch

End If


End Function
 
How are ya madelca100 . . .

Could'nt agree more with [blue]rubbernilly[/blue].
Code:
[blue]Public Function ReplaceinString(strSearch As Variant, _
                                strFind As String, _
                                strReplace As String) As Variant
   If IsNull(strSearch) Then
      ReplaceinString = ""
   Else
      ReplaceinString = Replace(strSearch, strFind, strReplace)
   End If
    
    
End Function[/blue]

Calvin.gif
See Ya! . . . . . .
 
And here my version:
Public Function myReplace(myString, myFind As String, myRepl As String)
If Trim(myString & "") <> "" Then
myReplace = Replace(myString, myFind, myRepl)
End If
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top