string.html
来自「perl教程」· HTML 代码 · 共 494 行 · 第 1/2 页
HTML
494 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Unicode::String - String of Unicode characters</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>Unicode::String - String of Unicode characters</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#methods">METHODS</a></li>
<li><a href="#class_methods">Class methods</a></li>
<li><a href="#encoding_methods">Encoding methods</a></li>
<li><a href="#string_operations">String Operations</a></li>
</ul>
<li><a href="#functions">FUNCTIONS</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Unicode::String - String of Unicode characters (UTF-16BE)</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">Unicode::String</span> <span class="string">qw(utf8 latin1 utf16be)</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$u</span> <span class="operator">=</span> <span class="variable">utf8</span><span class="operator">(</span><span class="string">"string"</span><span class="operator">);</span>
<span class="variable">$u</span> <span class="operator">=</span> <span class="variable">latin1</span><span class="operator">(</span><span class="string">"string"</span><span class="operator">);</span>
<span class="variable">$u</span> <span class="operator">=</span> <span class="variable">utf16be</span><span class="operator">(</span><span class="string">"\0s\0t\0r\0i\0n\0g"</span><span class="operator">);</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="variable">$u</span><span class="operator">-></span><span class="variable">utf32be</span><span class="operator">;</span> <span class="comment"># 4 byte characters</span>
<span class="keyword">print</span> <span class="variable">$u</span><span class="operator">-></span><span class="variable">utf16le</span><span class="operator">;</span> <span class="comment"># 2 byte characters + surrogates</span>
<span class="keyword">print</span> <span class="variable">$u</span><span class="operator">-></span><span class="variable">utf8</span><span class="operator">;</span> <span class="comment"># 1-4 byte characters</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>A <code>Unicode::String</code> object represents a sequence of Unicode
characters. Methods are provided to convert between various external
formats (encodings) and <code>Unicode::String</code> objects, and methods are
provided for common string manipulations.</p>
<p>The functions utf32be(), utf32le(), utf16be(), utf16le(), utf8(),
utf7(), latin1(), uhex(), <code>uchr()</code> can be imported from the
<code>Unicode::String</code> module and will work as constructors initializing
strings of the corresponding encoding.</p>
<p>The <code>Unicode::String</code> objects overload various operators, which means
that they in most cases can be treated like plain strings.</p>
<p>Internally a <code>Unicode::String</code> 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.</p>
<p>
</p>
<h2><a name="methods">METHODS</a></h2>
<p>
</p>
<h2><a name="class_methods">Class methods</a></h2>
<p>The following class methods are available:</p>
<dl>
<dt><strong><a name="item_stringify_as">Unicode::String->stringify_as</a></strong>
<dt><strong>Unicode::String->stringify_as( $enc )</strong>
<dd>
<p>This method is used to specify which encoding will be used when
<code>Unicode::String</code> objects are implicitly converted to and from plain
strings.</p>
</dd>
<dd>
<p>If an argument is provided it sets the current encoding. The argument
should have one of the following: "ucs4", "utf32", "utf32be",
"utf32le", "ucs2", "utf16", "utf16be", "utf16le", "utf8", "utf7",
"latin1" or "hex". The default is "utf8".</p>
</dd>
<dd>
<p>The <a href="#item_stringify_as"><code>stringify_as()</code></a> method returns a reference to the current encoding
function.</p>
</dd>
</li>
<dt><strong><a name="item_new">$us = Unicode::String->new</a></strong>
<dt><strong>$us = Unicode::String->new( $initial_value )</strong>
<dd>
<p>This is the object constructor. Without argument, it creates an empty
<code>Unicode::String</code> object. If an $initial_value argument is given, it
is decoded according to the specified <a href="#item_stringify_as"><code>stringify_as()</code></a> encoding, UTF-8
by default.</p>
</dd>
<dd>
<p>In general it is recommended to import and use one of the encoding
specific constructor functions instead of invoking this method.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="encoding_methods">Encoding methods</a></h2>
<p>These methods get or set the value of the <code>Unicode::String</code> object by
passing strings in the corresponding encoding. If a new value is
passed as argument it will set the value of the <code>Unicode::String</code>,
and the previous value is returned. If no argument is passed then the
current value is returned.</p>
<p>To illustrate the encodings we show how the 2 character sample string
of "祄" (micro meter) is encoded for each one.</p>
<dl>
<dt><strong><a name="item_utf32be">$us->utf32be</a></strong>
<dt><strong>$us->utf32be( $newval )</strong>
<dd>
<p>The string passed should be in the UTF-32 encoding with bytes in big
endian order. The sample "祄" is "\0\0\0\xB5\0\0\0m" in this encoding.</p>
</dd>
<dd>
<p>Alternative names for this method are <code>utf32()</code> and ucs4().</p>
</dd>
</li>
<dt><strong><a name="item_utf32le">$us->utf32le</a></strong>
<dt><strong>$us->utf32le( $newval )</strong>
<dd>
<p>The string passed should be in the UTF-32 encoding with bytes in little
endian order. The sample "祄" is is "\xB5\0\0\0m\0\0\0" in this encoding.</p>
</dd>
</li>
<dt><strong><a name="item_utf16be">$us->utf16be</a></strong>
<dt><strong>$us->utf16be( $newval )</strong>
<dd>
<p>The string passed should be in the UTF-16 encoding with bytes in big
endian order. The sample "祄" is "\0\xB5\0m" in this encoding.</p>
</dd>
<dd>
<p>Alternative names for this method are <code>utf16()</code> and ucs2().</p>
</dd>
<dd>
<p>If the string passed to <a href="#item_utf16be"><code>utf16be()</code></a> starts with the Unicode byte order
mark in little endian order, the result is as if <a href="#item_utf16le"><code>utf16le()</code></a> was called
instead.</p>
</dd>
</li>
<dt><strong><a name="item_utf16le">$us->utf16le</a></strong>
<dt><strong>$us->utf16le( $newval )</strong>
<dd>
<p>The string passed should be in the UTF-16 encoding with bytes in
little endian order. The sample "祄" is is "\xB5\0m\0" in this
encoding. This is the encoding used by the Microsoft Windows API.</p>
</dd>
<dd>
<p>If the string passed to <a href="#item_utf16le"><code>utf16le()</code></a> starts with the Unicode byte order
mark in big endian order, the result is as if <a href="#item_utf16le"><code>utf16le()</code></a> was called
instead.</p>
</dd>
</li>
<dt><strong><a name="item_utf8">$us->utf8</a></strong>
<dt><strong>$us->utf8( $newval )</strong>
<dd>
<p>The string passed should be in the UTF-8 encoding. The sample "祄" is
"\xC2\xB5m" in this encoding.</p>
</dd>
</li>
<dt><strong><a name="item_utf7">$us->utf7</a></strong>
<dt><strong>$us->utf7( $newval )</strong>
<dd>
<p>The string passed should be in the UTF-7 encoding. The sample "祄" is
"+ALU-m" in this encoding.</p>
</dd>
<dd>
<p>The UTF-7 encoding only use plain US-ASCII characters for the
encoding. This makes it safe for transport through 8-bit stripping
protocols. Characters outside the US-ASCII range are base64-encoded
and '+' is used as an escape character. The UTF-7 encoding is
described in RFC 1642.</p>
</dd>
<dd>
<p>If the (global) variable $Unicode::String::UTF7_OPTIONAL_DIRECT_CHARS
is TRUE, then a wider range of characters are encoded as themselves.
It is even TRUE by default. The characters affected by this are:</p>
</dd>
<dd>
<pre>
! " # $ % & * ; < = > @ [ ] ^ _ ` { | }</pre>
</dd>
</li>
<dt><strong><a name="item_latin1">$us->latin1</a></strong>
<dt><strong>$us->latin1( $newval )</strong>
<dd>
<p>The string passed should be in the ISO-8859-1 encoding. The sample "祄" is
"\xB5m" in this encoding.</p>
</dd>
<dd>
<p>Characters outside the "\x00" .. "\xFF" range are simply removed from
the return value of the <a href="#item_latin1"><code>latin1()</code></a> method. If you want more control
over the mapping from Unicode to ISO-8859-1, use the <code>Unicode::Map8</code>
class. This is also the way to deal with other 8-bit character sets.</p>
</dd>
</li>
<dt><strong><a name="item_hex">$us->hex</a></strong>
<dt><strong>$us->hex( $newval )</strong>
<dd>
<p>The string passed should be plain ASCII where each Unicode character
is represented by the "U+XXXX" string and separated by a single space
character. The "U+" prefix is optional when setting the value. The
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?