Kodewick
開始學 →
字串 string5 / 30

C++ 字串

第 5 課,共 30 課 · 第 1 部分 · 起步 · 約 15 分鐘

std::string 是 C++ 的字串型別:用 + 拼接,size() 求長度,s[i] 取字元,substr 擷取,find 查詢,找不到時返回 std::string::npos。

名字、位址、商品編號……文字在程式裡到處都是。C++ 用 std::string 放一串文字(要 #include <string>)。兩個字串用 + 就能接起來:

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string city = "上海";6    std::string street = "南京路";7    std::string address = city + street + " 100 號";8    std::cout << address << '\n';9    address += ",3 樓";10    std::cout << address << '\n';11    return 0;12}
輸出
上海南京路 100 號
上海南京路 100 號,3 樓

+ 把幾段文字連成一個新字串;+= 把一段文字接到原來那個的後面,和第 4 課裡數字的 += 一個意思。

要記住:+ 只管把文字接起來。字串裡寫的就算是數字,也不會被加起來。

猜輸出猜錯也沒關係

這段程式顯示什麼?

1#include <iostream>2#include <string>3 4int main() {5    std::string a = "12";6    std::string b = "3";7    std::cout << a + b << '\n';8    return 0;9}
練習 1

拼出全名

用 + 把 last 和 first 接起來,顯示 林小明,你好!。

要顯示
林小明,你好!
exercise1.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string first = "小明";6    std::string last = "林";7 8    return 0;9}
輸出
點「執行」或按 Ctrl+Enter

長度 size()

s.size() 告訴你字串有多長。.size() 寫在變數後面,是字串自己帶的成員函式,以後還會見到很多這樣的寫法。

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string code = "AB-1024";6    std::string name = "牛奶";7    std::cout << code.size() << '\n';8    std::cout << name.size() << '\n';9    return 0;10}
輸出
7
6

AB-1024 是 7 個字元,顯示 7。可是「牛奶」兩個字顯示的是 6:size() 數的是位元組,英文字母、數字、符號各佔 1 個位元組,一個漢字在這裡佔 3 個。所以這一課裡按位置取字元的例子,都只用英文字母和數字。

猜輸出猜錯也沒關係

這段程式顯示什麼?

1#include <iostream>2#include <string>3 4int main() {5    std::string s = "C++ 2026";6    std::cout << s.size() << '\n';7    return 0;8}

用下標取字元

字串裡的每個字元都有一個編號,叫下標,從 0 開始。s[0] 是第一個字元,s[s.size() - 1] 是最後一個。取出來的是 char。std::string 是可以改的:給 s[i] 賦值,就換掉了那個字元。

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string name = "jack";6    std::cout << name[0] << '\n';7    std::cout << name[name.size() - 1] << '\n';8    name[0] = 'J';9    std::cout << name << '\n';10    return 0;11}
輸出
j
k
Jack

點單步,看 name 從 jack 變成 Jack。注意 'J' 是單引號:name[0] 是一個字元,放進去的也得是一個字元。

0h
1e
2l
3l
4o

s[1] → 'e'

拖動 i:下標從 0 開始數

拖動 i:s[i] 就是下標 i 上的那個字元。下標從 0 開始,拖到 5 就出了字串
猜輸出猜錯也沒關係

這段程式顯示什麼?

1#include <iostream>2#include <string>3 4int main() {5    std::string w = "cat";6    w[0] = 'b';7    std::cout << w << " " << w[2] << '\n';8    return 0;9}

下標越界

"AB12" 只有 4 個字元,下標是 0 到 3。用 s[10] 去取一個不存在的字元,是未定義行為:普通的電腦上可能顯示一個亂七八糟的字元,也可能出別的事。我們的引擎會停在這一行:

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string code = "AB12";6    std::cout << code[3] << '\n';7    std::cout << code[10] << '\n';8    return 0;9}
輸出
2
index out of range

發生了什麼: 停在第 7 行:你訪問了 string[10],可它只有 4 個字元(下標 0~3)。

code[3] 是最後一個字元 2,沒問題;code[10] 越界了。(C++ 倒是允許讀 code[4]:它是字串結尾一個看不見的空字元,對你沒什麼用。)用下標之前,先想一想它是不是小於 size()。

單選

std::string s = "hello"; 哪個寫法取出的是最後一個字元 o?

substr 和 find

s.substr(start, n) 從下標 start 開始取 n 個字元,組成一個新字串;不寫 n 就一直取到最後。s.find("x") 找 x 第一次出現的下標。兩個配起來,就能把一個郵箱拆成兩半:

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string email = "[email protected]";6    auto at = email.find("@");7    std::cout << at << '\n';8    std::cout << email.substr(0, at) << '\n';9    std::cout << email.substr(at + 1) << '\n';10    return 0;11}
輸出
3
lin
example.com

@ 在下標 3。substr(0, 3) 是前 3 個字元 lin,substr(4) 是從下標 4 到最後。find 返回的型別名字很長,這裡正好用 auto。

lin@example.com
01234567891011121314
substr(0, 3)↑find("@")substr(4)
find 找到 @ 在下標 3;substr(0, 3) 取它前面的 3 個字元,substr(4) 取它後面的全部
猜輸出猜錯也沒關係

這段程式顯示什麼?

1#include <iostream>2#include <string>3 4int main() {5    std::string s = "banana";6    std::cout << s.find("an") << " " << s.substr(2, 3) << '\n';7    return 0;8}
練習 2

郵箱裡的使用者名稱

用 find 和 substr 取出 @ 前面的部分,顯示 使用者名稱:zhang.wei。

要顯示
使用者名稱:zhang.wei
exercise2.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string email = "[email protected]";6 7    return 0;8}
輸出
點「執行」或按 Ctrl+Enter

找不到:npos

s.find("x") 找到了,就返回下標。要是字串裡根本沒有 x 呢?找不到的時候,find 回傳一個特別的值 std::string::npos(「沒有這個位置」):

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string email = "lin.example.com";6    auto at = email.find("@");7    std::cout << (at == std::string::npos) << '\n';8    return 0;9}
輸出
1

顯示 1,表示「是真的」:沒找到。== 這一句外面要加括號,不然 << 會先算。第 6 課學了 if,就能寫「找到了怎樣,找不到怎樣」。

單選

字串 s 裡沒有字母 z。s.find("z") 返回什麼?

比較字串

== 看兩個字串是不是一模一樣,< 和 > 按字典的順序比,從第一個字元開始一個一個比:

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    std::string a = "apple";6    std::string b = "banana";7    std::cout << (a == "apple") << '\n';8    std::cout << (a < b) << '\n';9    std::cout << ("Zoo" < a) << '\n';10    return 0;11}
輸出
1
1
1

三行都是 1。最後一行可能讓你意外:比的是字元的編碼,所有大寫字母都排在小寫字母前面,所以 Zoo 比 apple 小。漢字也是按編碼比,不是按拼音或筆畫。

猜輸出猜錯也沒關係

這段程式顯示什麼?

1#include <iostream>2#include <string>3 4int main() {5    std::string a = "Cat";6    std::string b = "cat";7    std::cout << (a == b) << " " << (a < b) << '\n';8    return 0;9}

數字和字串互換

要把數接進字串,先用 std::to_string 把它變成字串;反過來,std::stoi 把一串數字變成 int(i 是 int 的意思):

main.cpp
1#include <iostream>2#include <string>3 4int main() {5    int count = 3;6    std::string msg = "購物車裡有 " + std::to_string(count) + " 件商品";7    std::cout << msg << '\n';8    std::string text = "42";9    int n = std::stoi(text);10    std::cout << n + 8 << '\n';11    return 0;12}
輸出
購物車裡有 3 件商品
50

text + "8" 會是 "428";std::stoi(text) + 8 才是 50。從輸入讀到的、從檔案裡拿到的數字常常是字串,算之前要先轉換。

猜輸出猜錯也沒關係

這段程式顯示什麼?

1#include <iostream>2#include <string>3 4int main() {5    std::string a = "7";6    int b = std::stoi(a);7    std::cout << a + "1" << " " << b + 1 << '\n';8    return 0;9}
練習 3

修好它:文字加數字

這段程式編譯不過。改好它,讓它顯示 共 5 件。

要顯示
共 5 件
exercise3.cpp
1#include <iostream>2#include <string>3 4int main() {5    int count = 5;6    std::string msg = "共 " + count + " 件";7    std::cout << msg << '\n';8    return 0;9}
輸出
點「執行」或按 Ctrl+Enter

這一課學會了

  • std::string 放一串文字;+ 把字串接起來,+= 接到後面。
  • s.size() 是位元組數:英文字元各佔 1 個,漢字在這裡各佔 3 個。
  • s[i] 取出下標 i 的字元,下標從 0 開始;字串可以改:s[0] = 'J';。
  • 下標越界(i 不小於 size())是未定義行為,引擎會停下。
  • s.substr(start, n) 取一段;s.find("x") 找位置,找不到是 std::string::npos。
  • ==、< 比較字串,按字元的編碼一個一個比,大寫字母排在小寫前面。
  • std::to_string(n) 把數變成字串,std::stoi(s) 把字串變成 int。
第 5 課學完了嗎?第 5 課,共 30 課 · 進度存在這個瀏覽器裡
下一課 · 第 6 課 · 15 分鐘if 和 switch讓 C++ 程式做選擇:比較運算子、if / else if / else、&& || !、bool 顯示成 1 和 0、= 寫成 == 的坑和 g++ 的警告、switch 和忘了 break 會怎樣。下一課 →
點按鈕會順便把這一課標記學完,然後開啟下一課。