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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing all non-alphanumeric characters from a string

Status
Not open for further replies.

profwannabe

Programmer
Jan 6, 2001
53
US
I need to use the string #ClientName# to generate subdirectories. In order to avoid a variety of potential problems, I wish to strip the string of all non-alphanumeric characters. Is there a relatively simple function for this, or should I loop through the string one character at a time? Also, how do I simply delete a character rather than replacing it? Finally, if I use Replace (or ReplaceList), how can I account for all possible non-alphanumeric characters?

Thanks in advance.
 
the "easiest" way i found to do so was to use the regular expression in javascript ... can you use javascript ? do you mind using reg exp ? if not, then let me know and i'll try to explain
 
I don't mind using javascript, although I would need some guidance.

I have tried the following with limited success:

<CFSET #attributes.centername# = ReReplace(attributes.centername,&quot;[[:punct:][:cntrl:]]&quot;,&quot;&quot;,&quot;all&quot;)>

However, if there is whitespace in the string, the output contains it as well. Also, I run into problems with # in the string. Is there some way I could account for these two characters initially?

 
i'm sorry i'm in the hurry here, all i can do is either ask you to wait for monday (or for some niiiiiiiiice tt member to answer) or try to read which is ns doc on regexps

but the more i think the less i'm sure that js is the solution in your case : it's easy to pass cf variable to jscript, but it's not really feasible the other way (because jscript is CLIENT side and cf SERVER side) - still, you can pass the jscript variable to any page and there, handle it with the querystring stuffs ... but as it sounds it'll end up beeing MORE COMPLICATED than your first idea of going char by char, so i would suggest ... NOT to use my solution ... :-(
 
Thanks anyway iza!

I am getting closer, although the code is getting more cumbersome as I have embedded a ReReplace in a Replace function. The spaces are now removed from the string as well:

<cfset attributes.centername = &quot; Ho &~ll and~! /\{ }=-+_$%^ Har t &quot;>

<cfset #attributes.centername# = Replace(ReReplace(attributes.centername,&quot;[[:punct:][:cntrl:]]&quot;,&quot;&quot;,&quot;all&quot;), &quot; &quot;,&quot;&quot;,&quot;all&quot;)>

Thus
Ho &~ll and~! /\{ }=-+_$%^ Har t
becomes
HollandHart

However, I am still stuck if there happens to be a # in the string: the first nonalphanumeric character following the # errors out. The error occurs in the first cfset statement rather than in the statement with the functions. The only solution I can think of is to replace # with ##, but there must be a simpler way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top