Multiple Replace in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JS

    Multiple Replace in a string

    Is it possible to do multiple replaces in a string?

    Example: I'm trying to replace all of the spaces with "%20" and "#" with
    "%23", etc. I also need to replace the "?", "!", " ' ", etc.

    I know I can do this with:
    thisString.Repl ace(" ", "%20")
    thisString.Repl ace("#", "%23")

    But, I'm trying to find a way to do this one line of code.

    Any Ideas?

    TIA

    JS


  • AlexS

    #2
    Re: Multiple Replace in a string

    Check Regex.Replace

    HTH
    Alex

    "JS" <nothere@northe re.com> wrote in message
    news:readnc9OvM zGhTiiRVn-vA@comcast.com. ..[color=blue]
    > Is it possible to do multiple replaces in a string?
    >
    > Example: I'm trying to replace all of the spaces with "%20" and "#" with
    > "%23", etc. I also need to replace the "?", "!", " ' ", etc.
    >
    > I know I can do this with:
    > thisString.Repl ace(" ", "%20")
    > thisString.Repl ace("#", "%23")
    >
    > But, I'm trying to find a way to do this one line of code.
    >
    > Any Ideas?
    >
    > TIA
    >
    > JS
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Multiple Replace in a string

      JS,
      Have you tried:[color=blue]
      > thisString.Repl ace(" ", "%20")
      > thisString.Repl ace("#", "%23")[/color]

      thisString = thisString.Repl ace(" ", "%20").Replace( "#", "%23").
      [color=blue]
      > Example: I'm trying to replace all of the spaces with "%20" and "#" with
      > "%23", etc. I also need to replace the "?", "!", " ' ", etc.[/color]
      Although you may be approaching enough replacements that I would consider
      using a StringBuilder instead. I would encapsulate the use of the
      StringBuilder into a function, or possibly even a class.

      Hope this helps
      Jay

      "JS" <nothere@northe re.com> wrote in message
      news:readnc9OvM zGhTiiRVn-vA@comcast.com. ..[color=blue]
      > Is it possible to do multiple replaces in a string?
      >
      > Example: I'm trying to replace all of the spaces with "%20" and "#" with
      > "%23", etc. I also need to replace the "?", "!", " ' ", etc.
      >
      > I know I can do this with:
      > thisString.Repl ace(" ", "%20")
      > thisString.Repl ace("#", "%23")
      >
      > But, I'm trying to find a way to do this one line of code.
      >
      > Any Ideas?
      >
      > TIA
      >
      > JS
      >
      >[/color]


      Comment

      Working...