Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to reverse a (Unicode) string

by Jim (Curate)
on Jan 09, 2011 at 22:50 UTC ( #881385=note: print w/ replies, xml ) Need Help??


in reply to How to reverse a (Unicode) string

Here's a way to reverse a Unicode string using the regular expression character class \X to match Unicode extended grapheme clusters:

my $edocinU = join '', reverse $Unicode =~ m/\X/g;

Here's a demonstration using Vietnamese (tiếng Việt) words:

#!perl

use strict;
use warnings;
use utf8;

binmode STDOUT, ':encoding(UTF-8)';

my $Moonshine = "Rượu đế";
my $enihsnooM = join '', reverse $Moonshine =~ m/\X/g;

print "$Moonshine\n";
print "$enihsnooM\n";

__END__
Rượu đế
ếđ uợưR

LATIN CAPITAL LETTER R
LATIN SMALL LETTER U
COMBINING HORN
LATIN SMALL LETTER O
COMBINING HORN
COMBINING DOT BELOW
LATIN SMALL LETTER U
SPACE
LATIN SMALL LETTER D WITH STROKE
LATIN SMALL LETTER E
COMBINING CIRCUMFLEX ACCENT
COMBINING ACUTE ACCENT

[I was forced to use <pre> tags instead of <code> tags here to display the actual Vietnamese characters rather than their HTML character entities.]


Comment on Re: How to reverse a (Unicode) string
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://881385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-09-21 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls