On Mar 3, 3:16*pm, "Al Dunbar" <aland...@newsgroup> wrote:
> "dsoutter" <webmasterhub....@newsgroup> wrote in message
> > On Mar 2, 4:11 pm, dsoutter <webmasterhub....@newsgroup> wrote:
> >> VBScript String Replace
> >> Remove or replace specific characters from a string. The article below
> >> provides a function in VBScript to remove or replace characters in a
> >> string.
> >> VBScript String Replace
> >> remove Illegal Characters from a string: VBScript String Replace
> >> VBScript replace characters in string.
> Here is how I would code this function if I ever needed such a thing:
> * * msgbox clean("C:\<test>&<done>")
> * * function clean (strtoclean)
> * * * * strtemp = strtoclean
> * * * * badchars =
> * * * * for each badchar in badchars
> * * * * * * select case badchar
> * * * * * * * * case "&": goodchar = " and "
> * * * * * * * * case ":": goodchar = "-"
> * * * * * * * * case else: goodchar = " "
> * * * * * * end select
> * * * * * * strtemp = replace( strtemp, badchar, goodchar )
> * * * * next
> * * * * clean = strtemp
> * * end function
> IMHO, this has the same result but the logic is somewhat simpler. What
> benefit would I get from switching from my version to yours?
> /Al- Hide quoted text -
> - Show quoted text -
Hi Al, the logic is simpler as you are using the replace() function to
perform the string replace, where the function provided takes the left
and right parts of a string, either side of an illegal character. In
many cases, your method would be more suitable mainly due to the
simpler logic, especially when all instances of each character are to
be processed in the same way.
As the method provided parses the string character by character, you
should have greater control over the output when more complex
operations need to be performed, such as removing or replacing a
character only if it within a specific context:
Eg. replace "&" with " and " if padded with spaces or other specific
character, or with a "+" if not
"something & something else" would become "something and something
"somethin&something else" would become "somethin+something else".
Eg. replace ":" only if NOT part of a url:
"the website is http://code-tips.com
" would remain "the website is http://code-tips.com
"See Here: http://code-tips.com
" would become "See Here http://code-tips.com
This would be achieved by either checking the previous 3-5 characters
when a ":" is found to see if it is in the context of a url or not
(http, https, ftp), or by checking the characters following the
current ":" is "//" which would indicate that the semicolon is part of
This functionality has not been included in the function provided, but
would be easy to implement, as the string is incrementally parsed and
manipulated using a numeric string position value relative to the
current position/character in the string.
There may also be differences in performance between the two methods,
as the function provided includes the code required to remove or
replace each of the specified characters without calling the replace()
function. I suspect that the replace function uses a similar approach
to replace the specified characters so any difference in performance
would be minimal, unless parsing a large string value. I haven't yet
tested this for performance differences.