3

How can I check if a string has any other character than the ones listed here: http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters

I would like to know if the string was inputted in any other language than English. Is finding special characters in the string the best way to detect non-english characters?

Any suggestion is appreciated.

Yeti
  • 5,628
  • 9
  • 45
  • 71
  • Are you trying to distinguish ASCII and UNICODE characters? page list only ASCII Printable character. – Imran Apr 28 '11 at 12:25

2 Answers2

4

This can be very easily done (and without looping through character by character) using Regular Expressions.

var reg:RegExp = /^[\x20-\x7E]*$/;
var str1:String = "The quick brown fox jumps over the lazy dog.";
var str2:String = "The quick bröwn fox jumps over the läzy dög.";

trace(reg.test(str1)); //true
trace(reg.test(str2)); //false
IQAndreas
  • 8,060
  • 8
  • 39
  • 74
2
function isASCIIPrintableString(str:String):Boolean {
    for (var i:int = 0; i < str.length; i++) {
        var ch:Number = str.charCodeAt(i);
        if (ch < 32 || ch > 126) {
            return false;
        }
    }

    return true;
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142