SDU Tools: AsciiOnly and DigitsOnly

This post covers another pair of useful functions that are part of the SDU Tools collection.

The first tool AsciiOnly deals with situations where you need to convert Unicode data (multi-byte data) to ASCII data (single byte). It processes each character and if it's not in the ASCII range, it replaces the character with the value that you supply.  If your second parameter is an empty string, you'll effectively strip out the non-ASCII characters, but your replacement can also be one or more characters.

Finally, it gives you an option to strip out any control characters, so this same tool could be used to strip control characters (like carriage returns, linefeeds, or tabs) from a string.

Here's are some examples:


You can see AsciiOnly in action here:

The second tool is very similar but is used to strip everything from a string that isn't a digit (ie: a character from 0 to 9). Apart from cleaning up numbers, this could be useful as part of cleaning up phone numbers.

It has an option to keep or remove a leading sign character (ie: a plus or a minus sign).  Again while this is useful for numbers, it could be used to keep a plus sign (international dialling prefix) at the start of a phone number.

Here are some examples:

You can see DigitsOnly in action here:

You can find out more about our free SDU Tools here:

Leave a Reply

Your email address will not be published. Required fields are marked *