⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strings.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Strings
Date: Wed, 29 Apr 2009 09:25:20 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/strings.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Strings</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3DPLEAC-Perl =
href=3D"index.html"=20
rel=3DPREVIOUS><LINK title=3DNumbers href=3D"numbers.html" =
rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/index.html">Prev</A></TD>=

    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/numbers.html">Next</A></T=
D></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DSTRINGS>1. Strings</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN14>Introduction</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$string =3D <FONT color=3D#00cd00>'\n'</FONT><FONT =
color=3D#00ffff>;</FONT>                     <FONT color=3D#bebebe># two =
characters, \ and an n
</FONT>$string =3D <FONT color=3D#00cd00>'Jon \'Maddog\' =
Orwant'</FONT><FONT color=3D#00ffff>;</FONT>  <FONT color=3D#bebebe># =
literal single quotes
#-----------------------------
</FONT>$string =3D <FONT color=3D#00cd00>"\n"</FONT><FONT =
color=3D#00ffff>;</FONT>                     <FONT color=3D#bebebe># a =
"newline" character
</FONT>$string =3D <FONT color=3D#00cd00>"Jon \"Maddog\" =
Orwant"</FONT><FONT color=3D#00ffff>;</FONT>  <FONT color=3D#bebebe># =
literal double quotes
#-----------------------------
</FONT>$string =3D <FONT color=3D#ff7f50>q</FONT><FONT =
color=3D#00cd00>/Jon 'Maddog' Orwant/</FONT><FONT =
color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># literal single quotes
#-----------------------------
</FONT>$string =3D <FONT color=3D#ff7f50>q</FONT><FONT =
color=3D#00cd00>[Jon 'Maddog' Orwant]</FONT><FONT =
color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># literal single quotes
</FONT>$string =3D <FONT color=3D#ff7f50>q</FONT><FONT =
color=3D#00cd00>{Jon 'Maddog' Orwant}</FONT><FONT =
color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># literal single quotes
</FONT>$string =3D <FONT color=3D#ff7f50>q</FONT><FONT =
color=3D#00cd00>(Jon 'Maddog' Orwant)</FONT><FONT =
color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># literal single quotes
</FONT>$string =3D <FONT color=3D#ff7f50>q</FONT><FONT =
color=3D#00cd00>&lt;Jon 'Maddog' Orwant&gt;</FONT><FONT =
color=3D#00ffff>;</FONT>   <FONT color=3D#bebebe># literal single quotes
#-----------------------------
</FONT>$a =3D &lt;&lt;<FONT color=3D#00cd00>"</FONT><FONT =
color=3D#ff7f50>EOF</FONT><FONT color=3D#00cd00>"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00cd00>This is a multiline here document
terminated by EOF on a line by itself
</FONT><FONT color=3D#ff7f50>EOF</FONT><FONT color=3D#bebebe>
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN17>Accessing Substrings</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT>$value =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, $offset, $count<FONT =
color=3D#00ffff>);</FONT>
$value =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, $offset<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, $offset, $count<FONT =
color=3D#00ffff>)</FONT> =3D $newstring<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, $offset<FONT color=3D#00ffff>)</FONT>   =
      =3D $newtail<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# get a 5-byte string, skip 3, then grab 2 8-byte strings, then the rest
</FONT><FONT color=3D#00ffff>(</FONT>$leading, $s1, $s2, $trailing<FONT =
color=3D#00ffff>)</FONT> =3D
    <FONT color=3D#98fb98>unpack</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"A5 x3 A8 A8 A*"</FONT>, =
$data<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe># split at five byte boundaries
</FONT><FONT color=3D#cdad00>@fivers</FONT> =3D <FONT =
color=3D#98fb98>unpack</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"A5"</FONT> <FONT color=3D#98fb98>x</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>length</FONT><FONT =
color=3D#00ffff>(</FONT>$string<FONT color=3D#00ffff>)</FONT>/<FONT =
color=3D#cdcd00>5</FONT><FONT color=3D#00ffff>)</FONT>, $string<FONT =
color=3D#00ffff>);</FONT>

<FONT color=3D#bebebe># chop string into individual characters
</FONT><FONT color=3D#cdad00>@chars</FONT>  =3D <FONT =
color=3D#98fb98>unpack</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"A1"</FONT> <FONT color=3D#98fb98>x</FONT> <FONT =
color=3D#98fb98>length</FONT><FONT color=3D#00ffff>(</FONT>$string<FONT =
color=3D#00ffff>)</FONT>, $string<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT>$string =3D <FONT color=3D#00cd00>"This is what you =
have"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#         +012345678901234567890  Indexing =
forwards  (left to right)
#          109876543210987654321- Indexing backwards (right to left)
#           note that 0 means 10 or 20, etc. above
</FONT>
$first  =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>0</FONT>, <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>);</FONT>  <FONT =
color=3D#bebebe># "T"
</FONT>$start  =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>5</FONT>, <FONT =
color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>);</FONT>  <FONT =
color=3D#bebebe># "is"
</FONT>$rest   =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>13</FONT><FONT =
color=3D#00ffff>);</FONT>    <FONT color=3D#bebebe># "you have"
</FONT>$last   =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>-1</FONT><FONT =
color=3D#00ffff>);</FONT>    <FONT color=3D#bebebe># "e"
</FONT>$end    =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>-4</FONT><FONT =
color=3D#00ffff>);</FONT>    <FONT color=3D#bebebe># "have"
</FONT>$piece  =3D <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>-8</FONT>, <FONT =
color=3D#cdcd00>3</FONT><FONT color=3D#00ffff>);</FONT> <FONT =
color=3D#bebebe># "you"
#-----------------------------
</FONT>$string =3D <FONT color=3D#00cd00>"This is what you =
have"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> $string<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#This is what you have
</FONT>
<FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>5</FONT>, <FONT =
color=3D#cdcd00>2</FONT><FONT color=3D#00ffff>)</FONT> =3D <FONT =
color=3D#00cd00>"wasn't"</FONT><FONT color=3D#00ffff>;</FONT> <FONT =
color=3D#bebebe># change "is" to "wasn't"
#This wasn't what you have
</FONT>
<FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>-12</FONT><FONT =
color=3D#00ffff>)</FONT>  =3D <FONT =
color=3D#00cd00>"ondrous"</FONT><FONT color=3D#00ffff>;</FONT><FONT =
color=3D#bebebe># replace last 12 characters
#This wasn't wondrous
</FONT>
<FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>0</FONT>, <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT> =3D <FONT =
color=3D#00cd00>""</FONT><FONT color=3D#00ffff>;</FONT>       <FONT =
color=3D#bebebe># delete first character
#his wasn't wondrous
</FONT>
<FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>-10</FONT><FONT =
color=3D#00ffff>)</FONT>  =3D <FONT color=3D#00cd00>""</FONT><FONT =
color=3D#00ffff>;</FONT>       <FONT color=3D#bebebe># delete last 10 =
characters
#his wasn'
#-----------------------------
# you can test substrings with =3D~
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>-10</FONT><FONT =
color=3D#00ffff>)</FONT> =3D~ <FONT color=3D#ff7f50>/</FONT><FONT =
color=3D#00cd00>pattern</FONT><FONT color=3D#ff7f50>/</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Pattern =
matches in last 10 characters\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#bebebe># substitute "at" for "is", restricted to first =
five characters
</FONT><FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$string, <FONT color=3D#cdcd00>0</FONT>, <FONT =
color=3D#cdcd00>5</FONT><FONT color=3D#00ffff>)</FONT> =3D~ <FONT =
color=3D#ff7f50>s</FONT><FONT color=3D#ff7f50>/</FONT><FONT =
color=3D#00cd00>is</FONT><FONT color=3D#ff7f50>/</FONT><FONT =
color=3D#00cd00>at</FONT><FONT color=3D#ff7f50>/</FONT><FONT =
color=3D#ff7f50>g</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# exchange the first and last letters in a string
</FONT>$a =3D <FONT color=3D#00cd00>"make a hat"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>(</FONT><FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$a,<FONT color=3D#cdcd00>0</FONT>,<FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>)</FONT>, <FONT =
color=3D#98fb98>substr</FONT><FONT color=3D#00ffff>(</FONT>$a,<FONT =
color=3D#cdcd00>-1</FONT><FONT color=3D#00ffff>))</FONT> =3D <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$a,<FONT color=3D#cdcd00>-1</FONT><FONT =
color=3D#00ffff>)</FONT>, <FONT color=3D#98fb98>substr</FONT><FONT =
color=3D#00ffff>(</FONT>$a,<FONT color=3D#cdcd00>0</FONT>,<FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>));</FONT>
<FONT color=3D#ff7f50>print</FONT> $a<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># take a ham
#-----------------------------
# extract column with unpack
</FONT>$a =3D <FONT color=3D#00cd00>"To be or not to be"</FONT><FONT =
color=3D#00ffff>;</FONT>
$b =3D <FONT color=3D#98fb98>unpack</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"x6 A6"</FONT>, $a<FONT =
color=3D#00ffff>);</FONT>  <FONT color=3D#bebebe># skip 6, grab 6
</FONT><FONT color=3D#ff7f50>print</FONT> $b<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># or not
</FONT>
<FONT color=3D#00ffff>(</FONT>$b, $c<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#98fb98>unpack</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"x6 A2 X5 A2"</FONT>, $a<FONT color=3D#00ffff>);</FONT> =
<FONT color=3D#bebebe># forward 6, grab 2; backward 5, grab 2
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"$b\n$c\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe># or
#
# be
#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>cut2fmt</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>my</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@positions</FONT><FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#cdad00>@_</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$template</FONT>   =3D <FONT =
color=3D#00cd00>''</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$lastpos</FONT>    =3D <FONT =
color=3D#cdcd00>1</FONT><FONT color=3D#00ffff>;</FONT>

⌨️ 快捷键说明

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