Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Removing all non-alphanumeric characters from a string

profwannabe (Programmer)
16 Feb 01 11:01
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.
iza (Programmer)
16 Feb 01 11:34
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
profwannabe (Programmer)
16 Feb 01 11:39
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,"[[:punct:][:cntrl:]]","","all")>

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?

iza (Programmer)
16 Feb 01 11:49
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 http://developer.netscape.com/docs/manuals/communicator/jsref/corea3.htm 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 ...
profwannabe (Programmer)
16 Feb 01 12:20
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 = "   Ho &~ll and~! /\{  }=-+_$%^  Har t   ">

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

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!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close