Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
Old 04-12-2007, 11:31 PM Multiples REPLACE
Average Talker

Posts: 22
Name: pepe
Trades: 0
Hi,
My links get generated from a SQL query. So I want to avoid the use of strange characters coming from the DB such: á , é / . ; etc.

I am using this nested REPLACE for doing it:
replace(replace(pCategoryDesc," ","-"),".","")
But this nested will be very longgggg an above all confusing.

Is there any other way or function to do this clearer?

Thanks
jmz2 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-13-2007, 02:45 AM Re: Multiples REPLACE
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use a regular expression to replace multiple chars with a blank


Code:
function ReplaceChar(strIn)
	dim objRE
	set objRE = New RegExp
	objRE.pattern = "á|é" 
	objRE.Global = True
	ReplaceChar= objRE.replace(strIn,"")
	set objRE = nothing
end function
Each character you want to strip out gets added to the pattern, and is seperated by the alternation delimiter "|" ( the "pipe" symbol or vertical bar)
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-13-2007, 02:49 AM Re: Multiples REPLACE
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
One small add-on to Chris' point: if for some reason you need to replace the vertical bar itself, make sure you add the escape character (\).

So: objRE.pattern = á|\||é (the first bar will be searched for, but the second bar will work as the "or" character.)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-13-2007, 07:02 AM Re: Multiples REPLACE
Average Talker

Posts: 22
Name: pepe
Trades: 0
Thanks for the code, it is very handy.
But I don't want to get rid of everything. For example é will becomes e.
I have been playing with the code but unable to achive what I want.
Could you please help me?

I am using this:
function ReplaceChar(strIn)
dim objRE
set objRE = New RegExp
objRE.Global = True
objRE.pattern = "a|e"
ReplaceChar= objRE.replace(strIn,"z")
objRE.pattern = "s| |\.|/"
ReplaceChar= objRE.replace(strIn,"-")
set objRE = nothing
end function

But for some reason the 'objRE.pattern = "a|e" ' is not working. Only work the second statement.

Last edited by jmz2; 04-13-2007 at 07:55 AM.. Reason: inserting the code
jmz2 is offline
Reply With Quote
View Public Profile
 
Old 04-13-2007, 11:15 AM Re: Multiples REPLACE
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
It will but you won't see it, because you overwrite the first return value with the second.

you would recode the function as

Code:
function ReplaceChar(strIn,pattern,replaceChar)
	dim objRE
	set objRE = New RegExp
	objRE.pattern = pattern
	objRE.Global = True
	ReplaceChar= objRE.replace(strIn,replaceChar)
	set objRE = nothing
end function
then call it as

stringvar = ReplaceChar(strIN,"a|e","z")

and so on

for multiple replace I use an array and a looping function

Code:
function ReplaceHTML(strItem)
dim i

for i = 0 to ubound(ReplaceCode)
	strItem = replace(strItem, ReplaceCode(i,1), ReplaceCode(i,0))
next
ReplaceHTML = strItem	
end function
section from the array
Code:
dim ReplaceCode(96,1)

ReplaceCode(77,0) = "[h1]"
ReplaceCode(77,1) = "<h1>"
ReplaceCode(78,0) = "[/h1]"
ReplaceCode(78,1) = "</h1>"
ReplaceCode(79,0) = "[h2]"
ReplaceCode(79,1) = "<h2>"
ReplaceCode(80,0) = "[/h2]"
ReplaceCode(80,1) = "</h2>"
dim replacecode() whatever size you need

the character to replace goes in array(n,0) the character to replace it with goes in array(n,1)
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-13-2007, 11:40 AM Re: Multiples REPLACE
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
Alternatively, you could use something like this after you do the first replace:

ReplaceChar = objRE.replace (ReplaceChar, "-")

You may find that easier to work with.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-13-2007, 02:36 PM Re: Multiples REPLACE
Average Talker

Posts: 22
Name: pepe
Trades: 0
Thank you very much guys.
jmz2 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Multiples REPLACE
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.65551 seconds with 12 queries