Tip

String Functions

Implode, Explode & Replace Substring
'String_Functions:

Option Public

Function Explode(inputString As String, delimiter As String) As Variant

Dim workingString As String
workingString = inputString

Dim array() As String
Redim array(0)

Dim pos As Integer
Dim nextPos As Integer
pos = Instr(workingString, delimiter)
While pos <> 0
array(Ubound(array)) = Left(workingString, pos - 1)
workingString = Right(workingString, Len(workingString) -
Len(delimiter) - pos + 1)
pos = Instr(workingString, delimiter)
Redim Preserve array(Ubound(array) + 1)
Wend
array(Ubound(array)) = workingString

Explode = array

End Function

Function Implode(inputArray As Variant, delimiter As String) As String

If Datatype(inputArray) = 8 Then
Implode = inputArray
Exit Function
End If

Forall strInput In inputArray
Implode = Implode + strInput + delimiter
End Forall

Implode = Left(Implode, Len(Implode) - Len(delimiter))

End Function

Function ReplaceSubstring(Byval strSource As String, strFind As String,
strReplace As String) As String

' ThisFunction searches the string passed in strSource for all
' occurences of the string passed in strFind. It then replaces
' the strFind with the specified strReplace. If an error occurs,
' a NULL value is returned ("").

On Error Goto TheEnd

Dim begin As Integer
Dim found As Integer
Dim strReplaceSource As String
begin = 1

If Strcompare(strFind, "") = 0 Or Strcompare(strSource, "") = 0 Then Goto
TheEnd

found = Instr(begin, strSource, strFind, 1)
While (found > 0)
begin = found + Len(strFind)
strReplaceSource = Left$(strSource, (found - 1)) & strReplace & _
Right$(strSource, Len(strSource) - (found + (Len(strFind) - 1)))
strSource = strReplaceSource
found = Instr(begin, strSource, strFind, 1)
Wend

ReplaceSubstring = strSource
Exit Function

TheEnd:
ReplaceSubstring = ""

End Function
Sub Initialize

End Sub

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.