perldoc > Unicode::String

๐Ÿ“› NAME

Unicode::String โ€” String of Unicode characters (UTF-16BE)

๐Ÿš€ Quick Reference

Use CaseCommandDescription
๐Ÿ†• Create from UTF-8$u = utf8("string")Construct Unicode::String from UTF-8 bytes
๐Ÿ†• Create from Latin-1$u = latin1("string")Construct from ISO-8859-1 bytes
๐Ÿ†• Create from UTF-16BE$u = utf16be("\0s\0t\0r")Construct from big-endian UTF-16 bytes
๐Ÿ”„ Convert to UTF-8$u->utf8Get string as UTF-8 encoded bytes
๐Ÿ”„ Convert to UTF-32BE$u->utf32beGet 4-byte-per-char representation
๐Ÿ“ Get length$u->lengthReturn number of characters (surrogates count as 2)
๐Ÿ”ข First char code$u->ordUnicode code point (0x0โ€“0x10FFFF), handles surrogates
โœ‚๏ธ Substring$u->substr($off, $len)Extract substring by position and length
๐Ÿ” Find substring$u->index($other, $pos)Locate position of substring, optionally from offset
๐Ÿงฌ Create from codeuchar(0x3B1)Build single-character string from Unicode code point
๐Ÿ”€ Fix byte order$u->byteswap if $u->ord == 0xFFFECorrect endianness using BOM detection
๐Ÿ“› Character name$u->nameGet official Unicode name of first character
โš™๏ธ Set default encodingUnicode::String->stringify_as("utf8")Set encoding for implicit string conversions
๐Ÿ”— Concatenate$u . $otherOverloaded . operator for concatenation
๐Ÿ” Repeat$u x $countOverloaded x operator for repetition

๐Ÿ“‹ SYNOPSIS

use Unicode::String qw(utf8 latin1 utf16be);

$u = utf8("string");
$u = latin1("string");
$u = utf16be("\0s\0t\0r\0i\0n\0g");

print $u->utf32be;   # 4 byte characters
print $u->utf16le;   # 2 byte characters + surrogates
print $u->utf8;      # 1-4 byte characters

๐Ÿ“– DESCRIPTION

A Unicode::String object represents a sequence of Unicode characters. Methods are provided to convert between various external formats (encodings) and Unicode::String objects, and methods are provided for common string manipulations.

The functions utf32be(), utf32le(), utf16be(), utf16le(), utf8(), utf7(), latin1(), uhex(), uchr() can be imported from the Unicode::String module and will work as constructors initializing strings of the corresponding encoding.

The Unicode::String objects overload various operators, which means that they in most cases can be treated like plain strings.

Internally a Unicode::String object is represented by a string of 2 byte numbers in network byte order (big-endian). This representation is not visible by the API provided, but it might be useful to know in order to predict the efficiency of the provided methods.

๐Ÿ”ง METHODS

๐Ÿ—๏ธ Class Methods

The following class methods are available:

๐Ÿ”ค Encoding Methods

These methods get or set the value of the Unicode::String object by passing strings in the corresponding encoding. If a new value is passed as argument it will set the value of the Unicode::String, and the previous value is returned. If no argument is passed then the current value is returned.

To illustrate the encodings we show how the 2 character sample string of "ยตm" (micro meter) is encoded for each one.

โœ‚๏ธ String Operations

The following methods are available:

๐Ÿ“ฆ FUNCTIONS

The following functions are provided. None of these are exported by default.

๐Ÿ“š SEE ALSO

Unicode::CharName, Unicode::Map8

http://www.unicode.org/

perlunicode

ยฉ๏ธ COPYRIGHT

Copyright 1997-2000,2005 Gisle Aas

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


# Some old code that is not used any more (because the methods are
# now implemented as XS) and which I did not want to throw away yet.

sub ucs4_inperl
{
    my $self = shift;
    unless (ref $self) {
        my $u = new Unicode::String;
        $u->ucs4($self);
        return $u;
    }
    my $old = pack("N*", $self->ord);
    if (@_) {
        $$self = "";
        for (unpack("N*", shift)) {
            $self->append(uchr($_));
        }
    }
    $old;
}

sub utf8_inperl
{
    my $self = shift;
    unless (ref $self) {
        # act as ctor
        my $u = new Unicode::String;
        $u->utf8($self);
        return $u;
    }

    my $old;
    if (defined($$self) && defined wantarray) {
        # encode UTF-8
        my $uc;
        for $uc (unpack("n*", $$self)) {
            if ($uc < 0x80) {
                # 1 byte representation
                $old .= chr($uc);
            } elsif ($uc < 0x800) {
                # 2 byte representation
                $old .= chr(0xC0 | ($uc >> 6)) .
                        chr(0x80 | ($uc & 0x3F));
            } else {
                # 3 byte representation
                $old .= chr(0xE0 | ($uc >> 12)) .
                        chr(0x80 | (($uc >> 6) & 0x3F)) .
                        chr(0x80 | ($uc & 0x3F));
            }
        }
    }

    if (@_) {
        if (defined $_[0]) {
            $$self = "";
            my $bytes = shift;
            $bytes =~ s/^[\200-\277]+//;  # can't start with 10xxxxxx
            while (length $bytes) {
                if ($bytes =~ s/^([\000-\177]+)//) {
                    $$self .= pack("n*", unpack("C*", $1));
                } elsif ($bytes =~ s/^([\300-\337])([\200-\277])//) {
                    my($b1,$b2) = (ord($1), ord($2));
                    $$self .= pack("n", (($b1 & 0x1F) << 6) | ($b2 & 0x3F));
                } elsif ($bytes =~ s/^([\340-\357])([\200-\277])([\200-\277])//) {
                    my($b1,$b2,$b3) = (ord($1), ord($2), ord($3));
                    $$self .= pack("n", (($b1 & 0x0F) << 12) |
                                        (($b2 & 0x3F) <<  6) |
                                        ($b3 & 0x3F));
                } else {
                    croak "Bad UTF-8 data";
                }
            }
        } else {
            $$self = undef;
        }
    }

    $old;
}

sub latin1_inperl
{
    my $self = shift;
    unless (ref $self) {
        # act as ctor
        my $u = new Unicode::String;
        $u->latin1($self);
        return $u;
    }

    my $old;
    # XXX: should really check that none of the chars > 255
    $old = pack("C*", unpack("n*", $$self)) if defined $$self;

    if (@_) {
        # set the value
        if (defined $_[0]) {
            $$self = pack("n*", unpack("C*", $_[0]));
        } else {
            $$self = undef;
        }
    }
    $old;
}
Unicode::String
๐Ÿ“› NAME ๐Ÿš€ Quick Reference ๐Ÿ“‹ SYNOPSIS ๐Ÿ“– DESCRIPTION
๐Ÿ”ง METHODS
๐Ÿ“ฆ FUNCTIONS ๐Ÿ“š SEE ALSO ยฉ๏ธ COPYRIGHT

Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-20 17:41 @2600:1f28:365:80b0:8802:8bb4:3873:328e
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^