ch02_03.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 415 行 · 第 1/2 页
HTM
415 行
<html><head><title>Strings (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_02.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch02_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.3. Strings</h2><p><a name="INDEX-143" /><a name="INDEX-144" />Strings are sequences of<a name="INDEX-145" />characters (like<tt class="literal">hello</tt>). Strings may contain any combination of anycharacters.<a href="#FOOTNOTE-48">[48]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-48" /><p>[48]Unlike C or C++, there's nothingspecial about the <a name="INDEX-146" />NUL character in Perl, because Perluses length counting, not a null byte, to determine the end of thestring.</p> </blockquote><p>The shortest possible string has no characters. The longest stringfills all of your available memory (although you wouldn't beable to do much with that). This is in accordance with the principleof "no built-in limits" that Perl follows at everyopportunity. Typical strings are printable sequences of letters anddigits and punctuation in the <span class="acronym">ASCII</span> 32 to<span class="acronym">ASCII</span> 126 range. However, the ability to have anycharacter in a string means you can create, scan, and manipulate rawbinary data as strings -- something with which many otherutilities would have great difficulty. For example, you could updatea graphical image or compiled program by reading it into a Perlstring, making the change, and writing the result back out.</p><p>Like numbers, strings have a literal representation, which is the wayyou represent the string in a Perl program.<a name="INDEX-147" /><a name="INDEX-148" />Literal strings come in two differentflavors: <em class="emphasis">single-quoted string literals</em> and<em class="emphasis">double-quoted string literals</em>.</p><a name="lperl3-CHP-2-SECT-3.1" /><div class="sect2"><h3 class="sect2">2.3.1. Single-Quoted String Literals</h3><p>A <em class="emphasis">single-quoted string literal</em><a name="INDEX-149" />is a sequence of characters enclosed in<a name="INDEX-150" /> <a name="INDEX-151" />singlequotes. The single quotes are not part of the stringitself -- they're just there to let Perl identify thebeginning and the ending of the string. Any character other than asingle quote or a backslash between the quote marks (includingnewline characters, if the string continues onto successive lines)stands for itself inside a string. To get a <a name="INDEX-152" /> <a name="INDEX-153" />backslash, put two backslashes in arow, and to get a single quote, put a backslash followed by a singlequote. In other words:</p><blockquote><pre class="code">'fred' # those four characters: f, r, e, and d'barney' # those six characters'' # the null string (no characters)'Don\'t let an apostrophe end this string prematurely!''the last character of this string is a backslash: \\''hello\n' # hello followed by backslash followed by n'hellothere' # hello, newline, there (11 characters total)'\'\\' # single quote followed by backslash</pre></blockquote><p>Note that the <tt class="literal">\n</tt><a name="INDEX-154" /> within a single-quoted string is notinterpreted as a newline, but as the two characters backslash and<tt class="literal">n</tt>. Only when the backslash is followed by anotherbackslash or a single quote does it have special meaning.</p></div><a name="lperl3-CHP-2-SECT-3.2" /><div class="sect2"><h3 class="sect2">2.3.2. Double-Quoted String Literals</h3><p>A <em class="emphasis">double-quoted string literal</em><a name="INDEX-155" />is similar to the strings you may haveseen in other languages. Once again, it's a sequence ofcharacters, although this time enclosed in <a name="INDEX-156" /> <a name="INDEX-157" />doublequotes. But now the backslash takes on its full power to specifycertain control characters, or even any character at all throughoctal and hex representations. Here are some double-quoted strings:</p><blockquote><pre class="code">"barney" # just the same as 'barney'"hello world\n" # hello world, and a newline"The last character of this string is a quote mark: \"""coke\tsprite" # coke, a tab, and sprite</pre></blockquote><p>Note that the double-quoted literal string<tt class="literal">"barney"</tt> means the same six-character string toPerl as does the single-quoted literal string<tt class="literal">'barney'</tt>. It's like what we saw with numericliterals, where we saw that <tt class="literal">0377</tt> was another wayto write <tt class="literal">255.0</tt>. Perl lets you write the literal inthe way that makes more sense to you. Of course, if you wish to use abackslash escape (like <tt class="literal">\n</tt> to mean a newlinecharacter), you'll need to use the double quotes.</p><p>The backslash can precede many different characters to mean differentthings (generally called a <em class="emphasis">backslashescape</em><a name="INDEX-158" />). Thenearly complete<a href="#FOOTNOTE-49">[49]</a> list of double-quoted<a name="INDEX-159" />string escapes is given in <a href="ch02_03.htm#lperl3-CHP-2-TABLE-1">Table 2-1</a>. </p><blockquote class="footnote"> <a name="FOOTNOTE-49" /><p>[49]Recent versions of Perl haveintroduced "Unicode" escapes, which we aren't goingto be talking about here.</p> </blockquote><a name="lperl3-CHP-2-TABLE-1" /><h4 class="objtitle">Table 2-1. Double-quoted string backslash escapes </h4><table border="1"><tr><th><p>Construct</p></th><th><p>Meaning</p></th></tr><tr><td><blockquote><pre class="code">\n</pre></blockquote></td><td><p>Newline</p></td></tr><tr><td><blockquote><pre class="code">\r</pre></blockquote></td><td><p>Return</p></td></tr><tr><td><blockquote><pre class="code">\t</pre></blockquote></td><td><p>Tab</p></td></tr><tr><td><blockquote><pre class="code">\f</pre></blockquote></td><td><p>Formfeed</p></td></tr><tr><td><blockquote><pre class="code">\b</pre></blockquote></td><td><p>Backspace</p></td></tr><tr><td><blockquote><pre class="code">\a</pre></blockquote></td><td><p>Bell</p></td></tr><tr><td><blockquote><pre class="code">\e</pre></blockquote></td><td><p>Escape (<span class="acronym">ASCII</span> escape character)</p></td></tr><tr><td><blockquote><pre class="code">\007</pre></blockquote></td><td><p>Any octal <span class="acronym">ASCII</span> value (here,<tt class="literal">007</tt> = bell)</p></td></tr><tr><td><blockquote><pre class="code">\x7f</pre></blockquote></td><td><p>Any hex <span class="acronym">ASCII</span> value (here, <tt class="literal">7f</tt> =delete)</p></td></tr><tr><td>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?