Generic Search And Replace Function For Strings

This function lets you search for a substring in a string and replace it with
another string value.
Option Public
Option Declare

Public Function replaceSubstring( Byval strSource As String, strFrom As String,
strTo As String ) As String
'strSource represents your string
'strFrom is the substring that will be replaced by strTo
'Syntax: ReplaceSubString( "UNC Mens Basketball", "Mens", "Womens" )

'Variable used to indicate position of occurance of strFrom in strSource
Dim intPosition As Integer

'Variable used to indicate start position for Mid()
Dim intStart As Integer

'Variable used to hold length of strSource
Dim intLengthSource As Integer

'Variable used to hold length of strFrom
Dim intLengthFrom As Integer

'Variable used to hold length of strTo
Dim intLengthTo As Integer

'Get length of "source" string
intLengthSource = Len( strSource )

'Get length of "from" string
intLengthFrom = Len( strFrom )

'Get length of "to" string
intLengthTo = Len( strTo )

'Set start position in string to first character
intStart = 1

'Check for an occurance of strFrom
If Not ( Instr( 1, strSource, strFrom ) = 0 ) Then

'Substring found proceed ...

'Continue as long as strFrom is encountered in strSource
Do While Not ( Instr( intStart, strSource, strFrom ) = 0 )

'In each loop, when strFrom is encountered, set strSource equal
'to characters before strFrom + strTo + characters after strFrom

'Get position of occurance of strFrom in strSource
intPosition = Instr( intStart, strSource, strFrom )

'replace strFrom w/ strTo
strSource = Mid( strSource, 1, intPosition - 1 ) & strTo & _
Mid( strSource, intPosition + intLengthFrom, intLengthSource )

'Change start so we advance in strSource string
intStart = intPosition + intLengthTo

Loop 'Not ( InStr( intStart, strSource, strFrom ) = 0 )

End If 'Not ( InStr( 1, strSource, strFrom ) = 0 ) Then

'Return value
ReplaceSubString = strSource

End Function

This was first published in November 2000

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.