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">-&gt;</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">-&gt;</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">-&gt;</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-&gt;stringify_as</a></strong>

<dt><strong>Unicode::String-&gt;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: &quot;ucs4&quot;, &quot;utf32&quot;, &quot;utf32be&quot;,
&quot;utf32le&quot;, &quot;ucs2&quot;, &quot;utf16&quot;, &quot;utf16be&quot;, &quot;utf16le&quot;, &quot;utf8&quot;, &quot;utf7&quot;,
&quot;latin1&quot; or &quot;hex&quot;.  The default is &quot;utf8&quot;.</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-&gt;new</a></strong>

<dt><strong>$us = Unicode::String-&gt;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 &quot;祄&quot; (micro meter) is encoded for each one.</p>
<dl>
<dt><strong><a name="item_utf32be">$us-&gt;utf32be</a></strong>

<dt><strong>$us-&gt;utf32be( $newval )</strong>

<dd>
<p>The string passed should be in the UTF-32 encoding with bytes in big
endian order.  The sample &quot;祄&quot; is &quot;\0\0\0\xB5\0\0\0m&quot; 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-&gt;utf32le</a></strong>

<dt><strong>$us-&gt;utf32le( $newval )</strong>

<dd>
<p>The string passed should be in the UTF-32 encoding with bytes in little
endian order.  The sample &quot;祄&quot; is is &quot;\xB5\0\0\0m\0\0\0&quot; in this encoding.</p>
</dd>
</li>
<dt><strong><a name="item_utf16be">$us-&gt;utf16be</a></strong>

<dt><strong>$us-&gt;utf16be( $newval )</strong>

<dd>
<p>The string passed should be in the UTF-16 encoding with bytes in big
endian order. The sample &quot;祄&quot; is &quot;\0\xB5\0m&quot; 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-&gt;utf16le</a></strong>

<dt><strong>$us-&gt;utf16le( $newval )</strong>

<dd>
<p>The string passed should be in the UTF-16 encoding with bytes in
little endian order.  The sample &quot;祄&quot; is is &quot;\xB5\0m\0&quot; 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-&gt;utf8</a></strong>

<dt><strong>$us-&gt;utf8( $newval )</strong>

<dd>
<p>The string passed should be in the UTF-8 encoding. The sample &quot;祄&quot; is
&quot;\xC2\xB5m&quot; in this encoding.</p>
</dd>
</li>
<dt><strong><a name="item_utf7">$us-&gt;utf7</a></strong>

<dt><strong>$us-&gt;utf7( $newval )</strong>

<dd>
<p>The string passed should be in the UTF-7 encoding. The sample &quot;祄&quot; is
&quot;+ALU-m&quot; 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>
   ! &quot; # $ % &amp; * ; &lt; = &gt; @ [ ] ^ _ ` { | }</pre>
</dd>
</li>
<dt><strong><a name="item_latin1">$us-&gt;latin1</a></strong>

<dt><strong>$us-&gt;latin1( $newval )</strong>

<dd>
<p>The string passed should be in the ISO-8859-1 encoding. The sample &quot;祄&quot; is
&quot;\xB5m&quot; in this encoding.</p>
</dd>
<dd>
<p>Characters outside the &quot;\x00&quot; .. &quot;\xFF&quot; 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-&gt;hex</a></strong>

<dt><strong>$us-&gt;hex( $newval )</strong>

<dd>
<p>The string passed should be plain ASCII where each Unicode character
is represented by the &quot;U+XXXX&quot; string and separated by a single space
character.  The &quot;U+&quot; prefix is optional when setting the value.  The

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?