用過 MFC 的開發者對 CString 應該印象深刻——它好用得讓人不太想回去用 C 字元陣列。但如果離開 MFC 框架,還有這麼順手的替代方案嗎?答案是有的,而且不必依賴任何非標準 API:標準 C++ 的 std::string 功能相當完整,足以應付絕大多數專案需求。以下把常用介面整理成一份速查。
使用 string 必須引入標頭檔。注意是 <string>,而非帶 .h 的 <string.h>——後者是 C 語言的頭檔。
#include <string> using std::string; using std::wstring; // 或者 using namespace std;
string 對應 char,wstring 對應 wchar_t,兩者介面完全一致。以下僅以 string 說明。
string(const char *s); // 以 C 字串 s 初始化 string(int n, char c); // 以 n 個字元 c 初始化
此外也支援預設建構與複製建構,以下寫法都正確:
string s1; string s2 = "hello"; string s3(s2);
當請求的長度超出可表達範圍時,會拋出 length_error 例外。
const char &operator[](int n) const; const char &at(int n) const; char &operator[](int n); char &at(int n);
operator[] 與 at() 都回傳第 n 個字元的位置,差別在於 at() 會做範圍檢查,越界時拋出 out_of_range;[] 則不檢查。這也是兩者的取捨:速度與安全的選擇。
const char *data() const; // 回傳非 null 結尾的 C 字元陣列 const char *c_str() const; // 回傳以 null 結尾的 C 字串 int copy(char *s, int n, int pos = 0) const; // 把從 pos 開始的 n 個字元複製到 s,回傳實際複製數量
int capacity() const; // 目前容量(不需重新配置即可容納的字元數) int max_size() const; // 可存放的最大長度 int size() const; // 目前長度 int length() const; // 目前長度(與 size 等價) bool empty() const; // 是否為空 void resize(int len, char c); // 改變長度為 len,不足處以 c 填補
string 重載了 operator>> 與 operator<<,可直接搭配串流使用。另有 getline 用於讀取整行:
getline(istream &in, string &s); // 從 in 讀入直到換行符號
string &operator=(const string &s); // 把 s 賦給目前字串 string &assign(const char *s); // 以 C 字串 s 賦值 string &assign(const char *s, int n); // 以 s 開頭的 n 個字元賦值 string &assign(const string &s); // 把 s 賦給目前字串 string &assign(int n, char c); // 以 n 個字元 c 賦值 string &assign(const string &s, int start, int n); // s 從 start 起 n 個字元 string &assign(const_iterator first, const_iterator last); // 迭代器區間
string &operator+=(const string &s); // 附加到結尾 string &append(const char *s); // 附加 C 字串 string &append(const char *s, int n); // 附加 C 字串前 n 個字元 string &append(const string &s); // 同 operator+= string &append(const string &s, int pos, int n); // s 從 pos 起 n 個字元 string &append(int n, char c); // 附加 n 個字元 c string &append(const_iterator first, const_iterator last); // 迭代器區間
bool operator==(const string &s1, const string &s2) const; // 運算子 ">", "<", ">=", "<=", "!=" 均已重載 int compare(const string &s) const; int compare(int pos, int n, const string &s) const; int compare(int pos, int n, const string &s, int pos2, int n2) const; int compare(const char *s) const; int compare(int pos, int n, const char *s) const; int compare(int pos, int n, const char *s, int pos2) const;
compare 大於時回傳 1,小於回傳 -1,相等回傳 0。
string substr(int pos = 0, int n = npos) const; // 回傳 pos 起 n 個字元 void swap(string &s2); // 交換兩個字串的值
int find(char c, int pos = 0) const; // 從 pos 起搜尋字元 c int find(const char *s, int pos = 0) const; // 從 pos 起搜尋 C 字串 s int find(const char *s, int pos, int n) const; // 搜尋 s 前 n 個字元 int find(const string &s, int pos = 0) const; // 從 pos 起搜尋 s // 成功回傳位置,失敗回傳 string::npos int rfind(char c, int pos = npos) const; // 由後向前搜尋 int rfind(const char *s, int pos = npos) const; int rfind(const char *s, int pos, int n) const; int rfind(const string &s, int pos = npos) const; int find_first_of(char c, int pos = 0) const; // 找到 s 中任一字元首次出現處 int find_first_of(const char *s, int pos = 0) const; int find_first_of(const char *s, int pos, int n) const; int find_first_of(const string &s, int pos = 0) const; int find_first_not_of(char c, int pos = 0) const; // 找到不在 s 中的字元 int find_first_not_of(const char *s, int pos = 0) const; int find_first_not_of(const char *s, int pos, int n) const; int find_first_not_of(const string &s, int pos = 0) const; int find_last_of(char c, int pos = npos) const; // 由後向前,行為同 find_first_of int find_last_of(const char *s, int pos = npos) const; int find_last_of(const char *s, int pos, int n) const; int find_last_of(const string &s, int pos = npos) const; int find_last_not_of(char c, int pos = npos) const; int find_last_not_of(const char *s, int pos = npos) const; int find_last_not_of(const char *s, int pos, int n) const; int find_last_not_of(const string &s, int pos = npos) const;
記憶訣竅:first/last 指的是搜尋方向,of/not_of 指的是「包含」或「排除」。例如 find_first_not_of("0123456789") 就是找第一個非數字字元,常用於驗證純數字輸入。
string &replace(int p0, int n0, const char *s);
// 刪除 p0 起 n0 個字元,在 p0 處插入 s
string &replace(int p0, int n0, const char *s, int n); // 插入 s 前 n 個
string &replace(int p0, int n0, const string &s);
string &replace(int p0, int n0, const string &s, int pos, int n); // s 從 pos 起 n 個
string &replace(int p0, int n0, int n, char c); // 插入 n 個字元 c
string &replace(iterator first0, iterator last0, const char *s);
string &replace(iterator first0, iterator last0, const char *s, int n);
string &replace(iterator first0, iterator last0, const string &s);
string &replace(iterator first0, iterator last0, int n, char c);
string &replace(iterator first0, iterator last0,
const_iterator first, const_iterator last);
// 以上以迭代器區間 [first0, last0) 取代
string &insert(int p0, const char *s); string &insert(int p0, const char *s, int n); string &insert(int p0, const string &s); string &insert(int p0, const string &s, int pos, int n); // 前四個在 p0 位置插入 s 中從 pos 起 n 個字元 string &insert(int p0, int n, char c); // p0 處插入 n 個字元 c iterator insert(iterator it, char c); // it 處插入字元 c,回傳插入後位置 void insert(iterator it, const_iterator first, const_iterator last); void insert(iterator it, int n, char c);
iterator erase(iterator first, iterator last); // 刪除 [first, last),回傳新位置 iterator erase(iterator it); // 刪除 it 指向的字元 string &erase(int pos = 0, int n = npos); // 刪除 pos 起 n 個字元
清空字串時,clear() 比 erase() 語意更清楚,兩者效果相同。
string 提供向前與向後遍歷的迭代器,語法近似指標但不做範圍檢查。以 string::iterator 或 string::const_iterator 宣告變數,後者不允許修改內容。
const_iterator begin() const; iterator begin(); // 回傳起始位置 const_iterator end() const; iterator end(); // 回傳最後一個字元之後的位置 const_iterator rbegin() const; iterator rbegin(); // 回傳最後一個字元的位置 const_iterator rend() const; iterator rend(); // 回傳第一個字元之前的位置
rbegin/rend 用於反向遍歷,搭配 string::reverse_iterator 與 string::const_reverse_iterator 使用。
引入 <sstream> 後,可用 istringstream 與 ostringstream 做字串與資料型別之間的轉換,這是 C++ 中比 sprintf 更型別安全的做法:
string input("hello,this is a test");
istringstream is(input);
string s1, s2, s3, s4;
is >> s1 >> s2 >> s3 >> s4;
// s1="hello,this", s2="is", s3="a", s4="test"
ostringstream os;
os << s1 << s2 << s3 << s4;
cout << os.str();
整理下來可以發現,std::string 的介面設計相当完整:建構、存取、賦值、連接、比較、搜尋、替換、插入、刪除、迭代、串流應有盡有。用得好,它與 CString 的差距並不大,而且因為是標準庫的一員,跨平台與跨編譯器的相容性遠優於任何框架專屬類別。
另外值得補充的是,C++11 之後字串操作又多了 string_view(C++17,零成本的唯讀檢視)、to_string/stoi 家族、以及 range-based for 迴圈,寫起來比上面這些早期介面更簡潔。若專案允許使用較新的標準,建議優先採用這些工具。