Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Replace all non-alphanumeric characters with underscore

Featured Replies

How would I write a calculation to replace all non-alphanumeric characters with an underscore?

 

For example

 

ABC 123 (hello.world!)

 

would become

 

ABC_123__hello_world__

Like this?

Substitute (
	Text ; 
	[ " " ; "_" ] ; 
	[ "(" ; "_" ] ; 
	[ "." ; "_" ] ; 
	[ "!" ; "_" ] ; 
	[ ")" ; "_" ]
)             

There are probably better approaches but two options are 1. Build a recursive function to run each character through alphanumeric filter and if empty replace with _ or 2. a long substitution string substituting each possible character with _. I'm not in front of a computer at the moment to right a cf example but look into recursive custom functions. That's the direction I would go.

How would I write a calculation to replace all non-alphanumeric characters with an underscore?

 

I think we need a better definition of what is a "non-alphanumeric character" - or possibly, what is an "alphanumeric character". I suspect there may be an awful lot of characters in both categories - too many to consider each one individually. Perhaps it woud be best if you explained what type of input is expected here and - even ore importantly - what is the purpose of this exercise.

  • Author

Recursive function is the answer. Here's what I came up with:

 

ReplaceOtherChars ( string ; ignorechars ; replacechars )

 

 

If ( Filter ( Left ( string ; 1 ) ; ignorechars ) = "" ; replacechar ; Left ( string ; 1 ) )
&
If ( Length ( string ) > 1 ;
ReplaceOtherChars ( Right ( string ; Length ( string ) - 1 ) ; ignorechars ; replacechar )
; "" )
 
Example: 
ReplaceOtherChars ( "ABC 123 (hello.world!)" ; "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ; "_" )
Output: ABC_123__hello_world__

The reason I needed this is because native AppleScript no longer works for changing printers in Mac OS X v 10.9 Mavericks because "Printer Setup Utility" is no longer included, so I've realized that rather than installing an old version of Printer Setup Utility, I can just use the lpoptions terminal command to change printers, but the printer name seems to require all non-alphanumeric characters replaced with underscores, otherwise the command fails. (I still want users to be able to enter & view their printer names normally so this custom function will be used each time the printer is changed)

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.