Author Topic: Replace ASP String Function  (Read 1890 times)

Offline admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 296
    • View Profile
Replace ASP String Function
« on: December 20, 2009, 03:01:52 PM »
Replace is a commonly used ASP function that allows you to replace one character or characters in a string with another character or characters.

It takes in three parameters:

* The string variable to operate on
* A character or characters to look for
* A character or characters to replace that with

The original string variable is not altered.

There are MANY reasons to use Replace. One of the most common ones is in fixing email problems when people submit an email address to you. People are notorious for adding extra spaces to the beginning, end or even middle of their email address. Many mailers choke on those extra spaces. So to get rid of all spaces - replacing them with "nothing" - you would use:

RecEmail = Replace(RecEmail, " ", "")

Another common use of replace is to take in a comment that a user has typed in and format it for web reading. Users type in returns, or CHR(13) into their text. However, web browsers of course ignore hard returns so the user's text all mushes together. To change all of those returns with the web command <P> which is the paragraph break, you would use:

UserComment = Replace(UserComment, CHR(13), "<P>")

Now when you display UserComment, there are paragraph breaks to nicely separate the user's paragraphs.