473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

One line replace for multiple targets?

Writing some ASP code and I need to take a string, representing a phone
number, and strip out some characters, namely spaces, brackets and hyphens.

I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

....but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number of
characters.

I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?
Feb 3 '06 #1
10 5628
Noozer wrote:
Writing some ASP code and I need to take a string, representing a
phone number, and strip out some characters, namely spaces, brackets
and hyphens.
I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

...but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger
number of characters.

I know that this can be done with regular expressions, but all the
examples I see online are several lines in length. Isn't there a
simple way to just say "remove any of these" or "replace any of these
with nothing" in a single line of code?

No
Sorry
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 3 '06 #2
"Noozer" <do*******@me.here> wrote in message
news:2xDEf.315369$tl.211704@pd7tw3no...
Writing some ASP code and I need to take a string, representing a phone
number, and strip out some characters, namely spaces, brackets and hyphens.
I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

...but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number of
characters.

I know that this can be done with regular expressions, but all the examples I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single line of code?


If you only want digits then here's one approach.

<html>
<head>
<title>numbonly.htm</title>
<script type="text/javascript">
function numbonly(that) {
var numb = that.value;
var what = "";
for (var i=0; i<numb.length; i++) {
var byte = numb.substr(i,1);
if (byte >= "0" && byte <= "9") what += byte;
}
that.value = what;
}
</script>
</head>
<body>
<center>
<form name="form1">
<input type="text" name="numb" size="20" maxlength="20"
value="(123) 456-7890" onblur="numbonly(this)">
</form>
</center>
</body>
</html>
Feb 3 '06 #3
>>
I know that this can be done with regular expressions, but all the examples
I see online are several lines in length. Isn't there a simple way to just
say "remove any of these" or "replace any of these with nothing" in a single
line of code?
<<

As already stated no there isn't but yet there is; use a function (the art
of abstraction is often
lost in the serial nature of ASP script).

Have the following function (and a range of other useful utilities) in an
ASP page that is included in the pages where you need them:-

Function RegExReplace(rsInput, rsPattern, rsReplace)

Dim oRegEx

Set oRegEx = New RegExp

oRegEx.Pattern = rsPattern
oRegEx.Global = True
RegExReplace = oRegEx.Replace(rsInput, rsReplace)

End Function

Now your stated case becomes:-

pNum = RegExReplace(pNum;" |-|\)|\)","")

and your other similar cases will be simply a matter of varing the pattern.
PS. where is 'microsoft.public.inetserver.iis.activeserverpages ' that you
cross posted to?
my newsgroup app complains that is doesn't exist

Anthony.
Feb 4 '06 #4
"Anthony Jones" <An*@yadayadayada.com> wrote in message
news:#h**************@tk2msftngp13.phx.gbl...

[snip]
PS. where is 'microsoft.public.inetserver.iis.activeserverpages ' that you cross posted to? my newsgroup app complains that is doesn't exist


"microsoft.public.inetserver.iis.activeserverpages "
exists and has had 588 posts since January 1, 2004.
Feb 4 '06 #5
Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.

Feb 16 '06 #6
Andyza wrote:
Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.
Oh wait! I AM wrong. Nested Replace statements will accomplish this on a
single line.

This pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")


can be done by:
pnum=replace(replace(replace(replace(pNum,")",""), "(",""),"-","")," ","")

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 16 '06 #7
Andyza wrote:
Bob, it's very rare, but this time you're wrong. See the message in
this thread by Anthony Jones. A Regular Expression is ideal for this
problem.

What are you talking about? Please quote the message to which you are
replying.
Oh wait, I remember now.

A Regular Expession may be ideal for this problem, but a regular expression
also takes at least 3 lines to use, one to instantiate the regexp object,
one to initialize the pattern, and one to execute the replace statement. So
it is not the answer to the question you asked.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Feb 16 '06 #8
Noozer wrote on 03 feb 2006 in microsoft.public.inetserver.asp.general:
Writing some ASP code and I need to take a string, representing a
phone number, and strip out some characters, namely spaces, brackets
and hyphens.

I could write

pNum=replace(pNum," ","")
pNum=replace(pNum,"-","")
pNum=replace(pNum,"(","")
pNum=replace(pNum,")","")

...but that's a bit of a pain. Especially since I'll be doing similar
replacements with other data and may have to strip out a larger number
of characters.

I know that this can be done with regular expressions, but all the
examples I see online are several lines in length. Isn't there a
simple way to just say "remove any of these" or "replace any of these
with nothing" in a single line of code?


execute function in vbs or js:
<%
pNum = pNumReplace(pNum)
%>

define in jscript:
<script runat=server language=jscript>
function pNumReplace(x) { return x.replace(/[ -()]+/g,'') }
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 16 '06 #9
> Now your stated case becomes:-

pNum = RegExReplace(pNum;" |-|\)|\)","")

and your other similar cases will be simply a matter of varing the pattern.


Maybe I'm reading it wrong, but shouldn't that be:

pNum = RegExReplace(pNum," |-|\(|\)","")

i.e.: " |-|\(|\)" instead of " |-|\)|\)"

The original example has two closing brackets instead of one opening
and one closing bracket.

And shouldn't there be a comma after pNum instead of a semicolon?

Feb 16 '06 #10
i.e.: " |-|\(|\)" instead of " |-|\)|\)" The original example has two closing brackets instead of one opening
and one closing bracket. And shouldn't there be a comma after pNum instead of a semicolon?


Your quite right thats a typo.

Should read:-

pNum = RegExReplace(pNum," |-|\(|\)","")
Another option would be:-
Function CharReplace(rsInput, rsChars, rsReplace)

Dim i

CharReplace = rsInput

For i = 1 To Len(rsChars)
CharReplace = Replace(CharReplace, Mid(rsChars, i, 1), rsReplace)
Next

End Function

pNum = CharReplace(pNum, " -()", "")

Personally I prefer the RegExp solution since I don't like to create a
destory strings if I can help it. But the above may perform better for
short rsChars.

My main point is that there is nearly all ways a single line solution.
Create a function.

Anthony.

Feb 16 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jon LaRosa | last post by:
I'm am trying to remove multiple line breaks ("\n"). Here's my code: $imageStr = trim($_POST); $imageStr = str_replace("\n\n", "\n", $imageStr); ---------- $_POST is a textarea field, and...
19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
8
by: DB2 Novice | last post by:
I am trying to use DB2 Control Centre (version 8.2) to load one flat file into multiple tables. However, I don't see the options in Control Centre that allows that. Anyone knows how to do this?...
2
by: A. Gupta | last post by:
I'm compiling a project that I want to have multiple targets (basically debug and optimized). I would like to just be able to type 'make debug' or 'make optimized' and have the makefile compile...
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
2
by: Ayoa | last post by:
I am working on some web based custom reports and i want to open multiple browser at the click of a button. So for example if i have 2 guids in my arraylist, i want to open 2 windows. protected...
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line). To achieve this I's like to operate with only the...
2
by: caspardh | last post by:
i was trying to code a morse code decipherer which workd fine except that the .txt file that i was converting had linebreaks built in, i cant get python to ignore the line breaks and i cant find any...
19
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
This is an example of the data; 2007/07/27 11:00:03 ARES_INDICATION 010.050.016.002 404.2.01 (6511) RX 74 bytes 2007/07/27 11:00:03 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13 48...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.