|
|
Title | Use Split to replace characters within parts of a string surrounded by quotes in Visual Basic 6 |
Description | This example shows how to use Split to replace characters within parts of a string surrounded by quotes in Visual Basic 6. |
Keywords | replace, Split, regular expression |
Categories | Algorithms, Strings |
|
|
When you click the Replace button, the program breaks the text into pieces separated by quotes. It examines the first character in the original string to see if it begins with a quote. If the string doesn't begin with a quote, the program adds the first separated piece of text to the result.
The program them loops through the other pieces of text, making the appropriate substitution for every other piece (the ones surrounded by quotes).
|
|
Private Sub cmdReplace_Click()
Dim pieces() As String
Dim i As Integer
Dim result As String
' Break into pieces delimited by ".
pieces = Split(txtInput.Text, """")
' Make i be the index of the first piece surrounded by
' quotes.
If Left$(txtInput.Text, 1) = """" Then
' It starts with a quote.
result = ""
i = 0
Else
' It doesn't start with a quote.
' Add the first part of the string.
result = pieces(0)
i = 1
End If
' Process the other pieces.
Do While i < UBound(pieces)
' Use the next piece surrounded by quotes.
result = result & """" & Replace(pieces(i), "/", _
"?") & """"
Debug.Print """" & pieces(i) & """"
i = i + 1
If i > UBound(pieces) Then Exit Do
' Use the next piece not surrounded by quotes.
result = result & pieces(i)
i = i + 1
Loop
' Display the result.
txtResult.Text = result
End Sub
|
|
|
|
|
|