Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Wednesday, January 07, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

The Replace Function
12/18/1999 12:00:00 AM

Probably one of the most used functions in ASP is the Replace function. It saves valuable time and headaches. Here are a couple tips on using this handy function.

Formatting HTML
Have you ever had to enter information from an HTML form and place it into a database memo field only to display the information back to the user and try to format it the way they put it into your form field?


You could use the "<PRE></PRE>" tags to format the data so it's displayed exactly as the user entered it. The only problem using this method is it won't wrap the text to the next line. It will just display the text as one line and not start a new line unless the user actually placed a carriage return in the entered data. This could really distort your page and cause some really serious formatting problems.


The way around this is to use the VBScript Replace function in your ASP code to change each occurrence of a carriage return line feed into the equivalent html tag. This way it will handle the data as any other html text and wrap it when it needs to be wrapped and place a "<P>" or "<BR>" tag where there needs to be one.


For example we look for the corresponding ascii character code and then replace it with the appropriate html tag:

strMemo = Replace(strMemo, Chr(13), "<P>")

Database Functions
The Replace function is a really nice function to have some other handy items for it are preventing your SQL statements from crashing when you try to insert a string into a field that contains an apostrophe, say a name like O'Hara. In order to filter for data like this use the Replace function to double them up:

strField = Replace(strField, "'", "''")

Now you have some simple insurance against one of the most common problems encountered by beginning ASP developers.


Building SQL Statements
Another item for using the Replace function, that I use is for capturing the space in an entered field for keyword searching and then replacing the space with the appropriate SQL code. Say a user enters two keywords in a search field like "ASP Engine", and you want to be able to do a search on a field for both of these values, or any of the values, or the exact phrase. How do you do this with minimal amount of code? Use the Replace statement:

Keywords = Replace(Request.Form("Keywords"), "'", "''") 
'my form value with the apostrophes filtered.
'now build the SQL code....
MySQL = "SELECT FROM MyDatabase WHERE "
MySQL = MySQL & "MyField LIKE '%" & _
Replace(Keywords, " ", "%' AND MyField LIKE '%") & "%'"

So what we did here is replace the space the user entered with an AND statement in order to make criteria that a field would have to have both keywords in it for it to be pulled. Now you could replace the AND with an OR and this way the statement will search for either of the words in the field.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 1/3/2007 12:13:00 AM by Anonymous
Comments: The replace function has more optional arguments like start,count,compare. I cant find the full code anywhere for the replace function :(
Left on 12/15/2006 5:49:51 AM by Anonymous
Comments: its the pits
Left on 12/15/2006 5:47:59 AM by Anonymous
Comments:
Left on 11/3/2006 3:27:33 AM by Anonymous
Comments: bad
Left on 6/27/2006 11:59:59 AM by Anonymous
Comments: I've been looking everywhere to find how to replace substrings!!
No ratings available.
Left on 6/26/2006 10:14:28 AM by Anonymous
Comments: Thanks, found solution to my problem thru this article.
No ratings available.
Left on 1/16/2006 2:15:11 PM by Anonymous
Comments: I would like a replace function to replace the " (double quote) function.
No ratings available.
Left on 4/24/2005 7:14:58 PM by Anonymous
Comments: exactly what I was looking for, thanks!
Left on 4/14/2005 9:51:02 AM by Anonymous
Comments: only partial explanantion of topic. where are optional arguaments and how to use?
Left on 2/10/2005 9:25:53 PM by Anonymous
Comments: fantastically helpful, I'd never used the replace before and couldn't figure out what the problem with my syntax was.
Left on 12/2/2004 3:44:26 PM by Anonymous
Comments: I always forget to this and I always look it up over and over again.
Left on 10/17/2004 10:10:07 AM by Anonymous
Comments: it sucks

Left on 7/15/2004 2:24:36 AM by Anonymous
Comments: So Kewl!!! Tanx ha bunchies... :) Cheers
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970