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

📄 string2.html

📁 关于 C++ 的历史无须我来介绍了
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

  
  <link rel="stylesheet" href="css/stdlayout.css" type="text/css">

  
  <link rel="stylesheet" href="css/print.css" type="text/css">

  
  <meta content="text/html; charset=gb2312" http-equiv="content-type">

  
  <title>使用 string 型态</title>
</head>


<body>

<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>

<h1><a href="CppGossip.html">C++ Gossip: 使用 string 型态</a></h1>

直接操作字元阵列来进行字串操作是比较低阶的行为,就如之前所说的,阵列本身对自己的长度没有意识,所以无法判断自己是否为空字串,而阵列也不能直接指定
给另一个阵列,所以您无法直接将字串指定给另一个字串,您也无法对两个字串直接进行串连的动作,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
str1[] = "text1";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
str2[] = "text2";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str1 =
str2; // error</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">cout
&lt;&lt; str1 + str2 &lt;&lt; endl; // error</span><br>

</div>

<br>

C++标准函式库提供了string类别,您可以使用这个类别来建立实例,并进行各项高阶的字串抽象行为,像是字串的指定、串接等,要使用string类
别,您要先含入string表头档:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">#include
&lt;string&gt;</span><br>

</div>

<br>

您可以使用三种方式来建立一个string类别的物件(object),例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string
str1;&nbsp; // 建立一个string物件,内容为空字串</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string
str2("caterpillar"); &nbsp;// 以字串常量建立字串</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string
str3(str2); // 以string实例建立字串</span><br>

</div>

<br>

第一个建构字串的方法会建立一个空字串,空字串也是字串,只是长度为0;第二个方法会以字面常量内容来建立string实例;第三个方法会“复制”
str2的内容,并建立一个新的string实例。<br>

<br>

您可以使用size()来测试字串长度,使用empty()来测试字串是否为空,使用==来比较两个字串的字元内容是否相同,例如:<br>

<br>

<pre>#include &lt;iostream&gt; <br>#include &lt;string&gt; <br>using namespace std; <br><br>int main() { <br>    string str1; <br>    string str2("caterpillar"); <br>    string str3(str2); <br>    <br>    if(str1.empty()) {<br>        cout &lt;&lt; "str1 为空字串" &lt;&lt; endl;<br>    }<br>    <br>    cout &lt;&lt; "str1 长度: " &lt;&lt; str1.size() &lt;&lt; endl;<br>    cout &lt;&lt; "str2 长度: " &lt;&lt; str2.size() &lt;&lt; endl;<br>    cout &lt;&lt; "str3 长度: " &lt;&lt; str3.size() &lt;&lt; endl;<br>    <br>    if(str1 == str2) {<br>        cout &lt;&lt; "str1 与 str2 内容相同" &lt;&lt; endl;<br>    }<br><br>    if(str3 == str2) {<br>        cout &lt;&lt; "str3 与 str2 内容相同" &lt;&lt; endl;<br>    }<br>    <br>    return 0; <br>}</pre>

<br>

<span class="postbody">执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">str1 为空字串<br>

str1 长度: 0<br>

str2 长度: 11<br>

str3 长度: 11<br>

str3 与 str2 内容相同</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

您可以将字串指定给另一个字串,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string str1("text1");</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string
str2("text2");</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str1 = str2;</span><br>

</div>

<br>

这个指定会将str1原本的字串记忆体空间释放,并重新配置足够容纳str2的记忆体空间,然后将str2的各个字元复制至str1;您也可以将一个C-
Style的字串指定给string,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string
name("caterpillar");</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[] =
"justin";</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">name = str;</span><br>

</div>

<br>

但是您不能将一个string实例指定给字元阵列,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-style: italic;"></span><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char str[] = "justin";</span><br style="font-family: Courier New,Courier,monospace;">

</div>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">string
name("caterpillar");</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str = name; //
error</span><br>

</div>

<br>

<br>

您也可以直接使用+运算子来串接字串,例如:<br>

<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str1 = str1 + str2;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">

<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">str1 = str1 + "\n";</span><br>

</div>

<br>

string的实例可以使用下标运算子[ ]指定索引来存取相对应位置的字元,就有如字元阵列的操作一般,例如:<br>

<br>

<pre>#include &lt;iostream&gt; <br>#include &lt;string&gt; <br>using namespace std; <br><br>int main() { <br>    string name("caterpillar");<br>    <br>    for(int i = 0; i &lt; name.size(); i++) {<br>        cout &lt;&lt; name[i] &lt;&lt; endl;<br>    }<br>    <br>    return 0; <br>}<br></pre>

<br>

<span class="postbody">执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">c<br>

a<br>

t<br>

e<br>

r<br>

p<br>

i<br>

l<br>

l<br>

a<br>

r</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

string类别所生成的物件拥有几个常用的方法(method),例如: <br>

<br>

<pre>#include &lt;iostream&gt; <br>#include &lt;string&gt; <br>using namespace std; <br><br>int main() { <br>    string str1; <br>    string str2("caterpillar"); <br>    string str3(str2); <br><br>    // assign: 指定字串 <br>    str1 = str1.assign(str2, 0, 5); <br>    cout &lt;&lt; "str1: " &lt;&lt; str1 &lt;&lt; endl; <br>    str1 = str1.assign("caterpillar", 5, 6); <br>    cout &lt;&lt; "str1: " &lt;&lt; str1 &lt;&lt; endl; <br><br>    str1 = ""; <br><br>    // append: 字串串接 <br>    str1 = str1.append(str2, 0, 5); <br>    str1 = str1.append(str3, 5, 6); <br>    cout &lt;&lt; "str1: " &lt;&lt; str1 &lt;&lt; endl; <br><br>    // find: 寻找字串位置 <br>    cout &lt;&lt; "寻找str1中的第一个pill: " <br>         &lt;&lt; str1.find("pill", 0) &lt;&lt; endl; <br><br>    // insert: 插入字串 <br>    cout &lt;&lt; "在str1插入字串***: " <br>         &lt;&lt; str1.insert(5, "***") &lt;&lt; endl; <br><br>    cout &lt;&lt; "str1长度: " &lt;&lt; str1.length() &lt;&lt; endl; <br>    <br>    return 0; <br>}<br></pre>

<br>

<span class="postbody">执行结果:</span><br>

<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);">str1: cater<br>

str1: pillar<br>

str1: caterpillar<br>

寻找str1中的第一个pill: 5<br>

在str1插入字串***: cater***pillar<br>

str1长度: 14</span></small><span style="color: rgb(255, 255, 255);"><br>

      </span></td>

    </tr>

  
  </tbody>
</table>

<br>

以下简单的说明上面几个使用到的方法: <br>

<br>

<table style="text-align: left; width: 100%;" border="1" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>

      <td style="background-color: rgb(204, 204, 204);"><small>方法</small></td>

      <td style="background-color: rgb(204, 204, 204);"><small>说明</small></td>

    </tr>

    <tr>

      <td><small>assign(string, start, num)</small></td>

      <td><small>从string的第start个字元取出num个字元来指定给另一字串物件。</small></td>

    </tr>

    <tr>

      <td><small>append(string, start, num)</small></td>

      <td><small>从string的第start个字元取出num个字元来附加至另一字串物件之后。</small></td>

    </tr>

    <tr>

      <td><small>find(string, 0)</small></td>

      <td><small>从引发find的字串物件第0个字元寻找是否有符合string的子字串。 </small></td>

    </tr>

    <tr>

      <td><small>insert(start, string)</small></td>

      <td><small>将string插入引发insert的字串物件第start个字元之后。</small></td>

    </tr>

    <tr>

      <td><small>length()</small></td>

      <td><small>传回字串的长度。 </small></td>

    </tr>

  
  </tbody>
</table>

<br>

以上的几个方法都是重载(Overload)多次的方法,另还有其它的方法,例如compare()等等,更多的参数与方法使用请参考C++相关线上文
件。 <br>

</body>
</html>

⌨️ 快捷键说明

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