Results 1 to 4 of 4

Thread: [RESOLVED] Replace All Duplicates in String

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Resolved [RESOLVED] Replace All Duplicates in String

    I have a method that builds up a comma separated list returned as a string.
    Unfortunately it sometimes returns stuff like
    Code:
    Tom,,,,,,Dick,,,,,,Harry
    I tried replace to return single commas e.g
    Code:
    Replace (strNames, ",,", ",")
    but it only replaces some of them. e.g.
    Code:
    Tom,,,Dick,,,Harry
    Is there a recursive ReplaceAll function or do I have to write my own?

  2. #2

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Re: Replace All Duplicates in String

    So far I am doing it multiple times to try and catch all commas (very error prone).
    vb Code:
    1. strNames = Replace (strNames, ",,", ",")
    2. strNames = Replace (strNames, ",,", ",")
    3. strNames = Replace (strNames, ",,", ",")
    4. strNames = Replace (strNames, ",,", ",")

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Replace All Duplicates in String

    yeah, that's pretty much how it works.... I would put it in a while loop though... so that While there are ",," in your string, do the replace... as soon as there are no more ",," in there, it'll stop the loop...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: [RESOLVED] Replace All Duplicates in String

    vb Code:
    1. Dim strNames As String
    2.     strNames = "Tom,,,,,,Dick,,,,,,Harry"
    3.     Do While InStr(1, strNames, ",,") > 0
    4.         strNames = Replace(strNames, ",,", ",")
    5.     Loop
    6.     Debug.Print strNames

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