Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace 1

Status
Not open for further replies.

mdr2273

Programmer
Sep 4, 2006
22
US
What is the syntax to replace a character in a string with another character or a blank? I want to replace the $ , and . someone might enter in a field I want to hold a currency amount.
 
Code:
Replace(Replace(Request.Form("myfield"),"$",""),".","")
And the general syntax for Replace is:
Code:
Replace([i]string, value to replace, replacement value[/i])
In your case Replace is being used twice to replace both $s and .s in the form field with nothing.
 
Hi,

If I wanted to replace several characters would I have to use replace replace...... for every one ? Or is there a neater way please?

Also is there a way to only allow Alphanumeric character in ASP/VB, I mean replace anything that is'nt a letter or number.

Thanks for any help.

Ant

[sub]"Nothing is impossible until proven otherwise"[/sub]​
 
I'm pretty sure that can be accomplished with regular expressions, but you'll have to look those up since I don't work with them very often :)
 
The most basic way to use a regular expression to remove characters (or replace them) is:
Code:
Dim regex
Set regex = New regExp
regex.Global = True
regex.IgnoreCase = True
regex.pattern = "\$\."

Dim myString = "abcdefg$$$hij.kl.m$..etc"
Response.Write regex.Replace(myString,"")

Since both . and $ are special characters for regular expressions, I had to use a \ to escape them. you can find some good tutorials on regular expressions here:
- Two part intro to regular expressions and using them with VBScript
- More details on building regular expressions

The first page of the first link is useful, the second page jumps too far ahead (in my opinion)
The second link goes through the characters and how to build patterns and ends with some inofrmation on using the Replace() method.

Hope this helps,
-T

 
Excellent!

Thanks all.

[sub]"Nothing is impossible until proven otherwise"[/sub]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top