[RESOLVED] Replace text between quotes - VB6
I tried to search everywhere but couldn't find a way to solve easily.
I simply want to replace spaces with "*" between double quotes only.
So the below text
"us i hi ""n stu"" hi ki ""ji kh kj"" dfd df"
should yield
"us i hi ""n*stu"" hi ki ""ji*kh*kj"" dfd df"
Assume the quotes will be even count.
Thanks
Re: Replace text between quotes - VB6
How about Replace(string, " ", "*")?
Dave
Re: Replace text between quotes - VB6
Nope. I am not looking to replace all but the ones between the quotes. Just like google search
Re: Replace text between quotes - VB6
Sorry - I misread what you wanted.
Re: Replace text between quotes - VB6
vsusi
It's quite the brute force method, but this seems to work
Code:
'
tx = "us i hi ""n stu"" hi ki ""ji kh kj"" dfd df"
' 1 2 3 3
' 123456789012345678901234567890123456789
' us i hi "n stu" hi ki "ji kh kj" dfd df
zz = Len(tx)
' pass 1 - count occurrences, populate array with locations
Dim tx1 As String
Dim qt(10, 2)
nn = 0
cc = 0
For ii = 1 To zz
ch = Asc(Mid(tx, ii, 1))
If ch = 34 Then
nn = nn + 1
resu = nn Mod 2
If resu = 1 Then ' odd - is a beginning quote
cc = cc + 1
qt(cc, 1) = ii
ElseIf resu = 0 Then ' even - is an ending quote
qt(cc, 2) = ii
End If
End If
Next ii
' pass 2 - replace space with *
tx1 = ""
pp = 1
For ii = 1 To cc
' copy orig up to this point
For kk = pp To qt(ii, 1) - 1
tx1 = tx1 + Mid(tx, kk, 1)
pp = pp + 1
Next kk
' in 'positions-of-interest', replace spaces
For jj = qt(ii, 1) To qt(ii, 2)
ch = Asc(Mid(tx, jj, 1))
If ch <> 32 Then
tx1 = tx1 + Mid(tx, jj, 1)
pp = pp + 1
Else
tx1 = tx1 + "*"
pp = pp + 1
End If
Next jj
Next ii
' pass 3 - append remainder
For ii = qt(cc, 2) + 1 To zz
tx1 = tx1 + Mid(tx, ii, 1)
Next ii
Code:
orig -- tx = us i hi "n stu" hi ki "ji kh kj" dfd df
resu -- tx1 = us i hi "n*stu" hi ki "ji*kh*kj" dfd df
Hope that's close to what you were looking for
Holler if you have any questions
Spoo
Re: Replace text between quotes - VB6
Here's another way.
Code:
Const QUOTE = """"
Dim strData As String
Dim strParts() As String
Dim intIndex As Integer
' Build test data
strData = QUOTE & "us i hi " & QUOTE & QUOTE & "n stu" & QUOTE & QUOTE & " hi ki " & QUOTE & QUOTE & "ji kh kj" & QUOTE & QUOTE & "dfd df" & QUOTE
' Split the data at the double quotes
strParts = Split(strData, QUOTE & QUOTE)
' Replace spaces in certain parts
For intIndex = 0 To UBound(strParts)
If Left$(strParts(intIndex), 1) <> QUOTE And Left$(strParts(intIndex), 1) <> " " And Right$(strParts(intIndex), 1) <> QUOTE Then
strParts(intIndex) = Replace(strParts(intIndex), " ", "*")
End If
Next
' Reuse strData so it needs to be cleared
strData = ""
' Put the data back together
For intIndex = 0 To UBound(strParts)
If intIndex < UBound(strParts) Then
strData = strData & strParts(intIndex) & QUOTE & QUOTE
Else
strData = strData & strParts(intIndex)
End If
Next
Debug.Print strData
Re: Replace text between quotes - VB6
How about this
Code:
s = "us i hi ""n stu"" hi ki ""ji kh kj"" dfd df"
e = 1
For n1 = 1 To Len(s)
q1 = InStr(e, s, """") ' or q1 = InStr(e, s, Chr(34))
q2 = InStr(q1 + 1, s, """") ' or q2 = InStr(q1 + 1, s, Chr(34))
If q1 = 0 Or q2 = 0 Then Exit For
For n = q1 To q2
If Mid(s, n, 1) = " " Then Mid(s, n, 1) = "*"
Next n
e = n
n1 = e - 1
Next n1
Before: s = "us i hi ""n stu"" hi ki ""ji kh kj"" dfd df"
After: s = "us i hi ""n*stu"" hi ki ""ji*kh*kj"" dfd df"
Re: Replace text between quotes - VB6
Yet another way :)
Code:
Option Explicit
Private Sub Command_Click()
Dim intFile As Integer
Dim strData As String
Dim intStart As Integer
Dim intPos As Integer
Dim intPos1 As Integer
intFile = FreeFile
Open "C:\Test.txt" For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
intStart = 1
Do
intPos = InStr(intStart, strData, Chr(34) & Chr(34))
If intPos > 0 Then
intPos1 = InStr(intPos + 2, strData, Chr(34) & Chr(34))
If intPos1 > 0 Then
If intPos1 > intPos + 2 Then
Mid$(strData, intPos + 2, intPos1 - (intPos + 2)) = Replace(Mid$(strData, intPos + 2, intPos1 - (intPos + 2)), " ", "*")
End If
End If
End If
intStart = intPos1 + 2
Loop Until intStart > Len(strData) Or intPos = 0 Or intPos1 = 0
Debug.Print strData
End Sub
Re: Replace text between quotes - VB6
A slight modification to my previous example.
Code:
s = "us i hi ""n stu"" hi ki ""ji kh kj"" dfd df"
e = 1
For n1 = 1 To Len(s)
q1 = InStr(e, s, """")
q2 = InStr(q1 + 1, s, """")
If q1 = 0 Or q2 = 0 Then Exit For
Mid(s, q1 + 1, q2 - (q1 + 1)) = Replace(Mid(s, q1 + 1, q2 - (q1 + 1)), " ", "*")
e = q2 + 1
n1 = e - 1
Next n1
Re: Replace text between quotes - VB6
Thanks all for your help. I want to thank all of you who put efforts to do this code.
I am amazed to see how the below code is even possible. AFAIK Mid() can only return a value but how is it used to receive a value? Thanks
vb Code:
Mid(s, q1 + 1, q2 - (q1 + 1)) = Replace(Mid(s, q1 + 1, q2 - (q1 + 1)), " ", "*")
Re: Replace text between quotes - VB6
Quote:
Originally Posted by
vsusi
I am amazed to see how the below code is even possible. AFAIK Mid() can only return a value but how is it used to receive a value?
There's basically a Mid Function, used to the right of an assignment, which returns a sub-string from a string, and a Mid Statement, used on the left of an assignment, which sets a sub-string to a specific value.
Re: Replace text between quotes - VB6
Re: [RESOLVED] Replace text between quotes - VB6
AFAIK Mid() can only return a value but how is it used to receive a value? Thanks
It's always been like that
I don't know if you know the C language but it allows you to index a string making it easy to access characters within that string but VB does not so VB has the Left$, Mid$, and Right$ functions to manipulate a string.