Results 1 to 13 of 13

Thread: [RESOLVED] Replace text between quotes - VB6

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    MD
    Posts
    182

    Resolved [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
    Last edited by vsusi; May 18th, 2011 at 09:17 AM.

  2. #2
    Addicted Member thedoc1's Avatar
    Join Date
    Jan 2002
    Location
    Scenic Tonawanda, NY
    Posts
    143

    Re: Replace text between quotes - VB6

    How about Replace(string, " ", "*")?

    Dave
    Thanks

    Doc

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    MD
    Posts
    182

    Re: Replace text between quotes - VB6

    Nope. I am not looking to replace all but the ones between the quotes. Just like google search

  4. #4
    Addicted Member thedoc1's Avatar
    Join Date
    Jan 2002
    Location
    Scenic Tonawanda, NY
    Posts
    143

    Re: Replace text between quotes - VB6

    Sorry - I misread what you wanted.
    Thanks

    Doc

  5. #5
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    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

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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"
    Last edited by jmsrickland; May 18th, 2011 at 12:43 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
    Last edited by Doogle; May 18th, 2011 at 12:36 AM. Reason: Catered for two double quotes pairs next to each other

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
    Last edited by jmsrickland; May 18th, 2011 at 12:39 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    MD
    Posts
    182

    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:
    1. Mid(s, q1 + 1, q2 - (q1 + 1)) = Replace(Mid(s, q1 + 1, q2 - (q1 + 1)), " ", "*")

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Replace text between quotes - VB6

    Quote Originally Posted by vsusi View Post
    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.
    Last edited by Doogle; May 18th, 2011 at 08:08 AM. Reason: Removed incorrect edit

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    MD
    Posts
    182

    Re: Replace text between quotes - VB6

    Thanks all.

  13. #13
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.
    Last edited by jmsrickland; May 18th, 2011 at 11:23 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width