harangzsolt33 has asked for the wisdom of the Perl Monks concerning the following question:

What's the best way to tell if a string is unicode or plain ASCII, or do I just use a regex like this?:
if ($STRING =~ m/[^\x00-\xFF]{1}/) { ... }

OR MAYBE:

if (length($STRING) > ($STRING =~ tr|\x00-\xFF|\x00-\xFF|)) { ... }

I can't think of anything better. Maybe this, but I bet it's slow:

sub isUnicode { my $L = defined $_[0] ? length($_[0]) : 0; for (my $i = 0; $i < $L; $i++) { ord(substr($_[0], $i, 1)) < 256 or return 1; } return 0; }

Replies are listed 'Best First'.
Re: How to test if a string is unicode string?
by jwkrahn (Abbot) on Apr 04, 2025 at 05:26 UTC
    if ( $STRING =~ m/[^[:ascii:]]/ ) { print "Not ACSII\n" }
    Naked blocks are fun! -- Randal L. Schwartz, Perl hacker
Re: How to test if a string is unicode string?
by cavac (Prior) on Apr 04, 2025 at 12:11 UTC

    Technically, there are any number of ways to check if the string in Perl has bytes with a number greater than 255 or 127.

    You can check if bytes > 127 exists, which is outside the standard ASCII range. This may indicate some sort of Unicode or UTF encoded string. Or it may indicate some "extended ASCII" encoding where characters 128-255 represent some language/area specific encoding like german Umlauts or spanish punctuations (żĄ) or DOS "graphical" symbols. Or it may indicate binary data.

    It's noteworthy that UTF8 encodes data by setting the highest bit in a byte, but Unicode also uses the range 128-255 for symbols.

    So, there is no absolutely foolproof way to guess if you got Unicode, UTF-encoded stuff or some other data (either encoded or not). This is all rather unfortunate and can be a huge pain in the buttocks (speaking from experience). You basically have to track that yourself in your program alongside your data.

    See also:

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
    Also check out my sisters artwork and my weekly webcomics
      Hmm... Interesting. Thank you!
Re: How to test if a string is unicode string?
by ikegami (Patriarch) on Apr 04, 2025 at 15:36 UTC

    All those are equivalent.

    The fastest way of writing it is probably

    $STRING =~ tr/\x00-\xFF//c # Can be used as a boolean.

    But do they do what you asked?


    Does it check if it's plain ASCII?

    No. ASCII only has 128 characters, so the following would be closer:

    sub isASCII { !!( $_[0] =~ tr/\x00-\x7F//c ) }

    (!! just normalizes the result to be the true (&PL_sv_yes) or the false (&PL_sv_no).)

    But does it work?

    Not quite. It will always return true when provided ASCII, but it won't necessarily return false is provided non-ASCII.

    my $ascii = encode( "ASCII", "\N{LATIN SMALL LETTER A}" ); isASCII( $ascii ) # true (OK. true positive) my $decoded_text = "\N{U+2660}"; isASCII( $decoded_text ) # false (OK. true negative) my $temperature = 18; my $data_packet = pack( 's>', $temperature ); isASCII( $data_packet ) # true (Error. false positive)

    Does it check if it's plain Unicode?

    What does "Unicode" mean?

    • Decoded text?
    • UTF-8?
    • UTF-16?
    • A string stored using the upgraded storage format?

    Whichever one you want to do, it will run unto the same problem as checking if a string is ASCII: You can't eliminate false positives. (Well except checking the storage format. That can be done reliably, but relying on the result is a bug.)


    You can't tell if a string is ASCII or "Unicode" (whichever definition) reliably. If your code needs to deal with strings in multiple formats and distinguish between them, it will need to be told of the format of the strings.