1. 頭檔案
2. 定義
- string類型是存儲在記憶體中的連續的字元串,它隐藏了字元串的數組性質,可當做普通變量處理
3. 對象建立
方法 | 說明 |
---|---|
string str; | 建立一個空的string對象。原型:string() |
string str(“hello”); | 初始化指向”hello”字元串。原型string(const char* s) |
string str(10, ‘a’); | 包含n個char元素。原型:string(size_type n, char c) |
string st1(“hello”); string st2(str1); | 拷貝構造函數。原型:string(const string& str) |
string str1(“hello”); string str2 = str1; | 拷貝構造函數。重載運算符 = |
string str = “hello”; | C風格的string對象初始化 |
4. 常用方法
- 長度
方法 | 說明 |
---|---|
int strLen1 = str.length(); | C語言習慣 |
int strLen2 = str.size(); | STL容器 |
str.empty(); | 是否為空 |
- 添加
方法 | 說明 |
---|---|
+: string str1(“hello ”); string str2(“world\n”); string str3 = str1 + str2; | 拼接字元串 |
+=: string str1(“hello”); string str2(“world”); str1 += str2; | 拼接字元串 |
string str1(“hello”); string str2(“world”); str1.append(str2); str1.append(“oh”); | append()方法 |
string str(“123”); str.push_back(‘4’); | “1234”。push_back(),末尾添加 |
string str(“123”); str.pop_back(); | “12”。pop_back(): 末尾删除 |
- 比較:compare()函數
方法 | 說明 |
---|---|
== 0 | 相等 |
< 0 | 小于(指定的字元開始)或 更短 |
> 0 | 大于 或 更長 |
string str1("hello"), str2("hello, world");
int ret = str1.compare(str2);
ret = str1.compare(0, 2, str2); // str1從0開始的2個元素與str2比較
ret = str1.compare(1, 2, str2, 0, 3); // str1從1開始的2個元素與str2從0開始的3個元素比較
ret = str1.compare("Hello"); // 與字元串"Hello"比較
ret = str1.compare(1, 2, "Hello"); // str1從1開始的2個元素與"Hello"比較
ret = str1.compare(1, 2, “Hello”, 1, 2);// str1從1開始的2個元素比"Hello"子串(1,2)比較
- 通路
string str("Hello,World!");
cout << str[1] << endl; // 下标方式通路
cout << str.at(1) << endl; // at方式通路
- 子串
/* 函數原型 */
string substr(size_t pos = 0, size_t len = npos) const; // 從pos開始的len個元素
string str("Hello,World!");
string subStr = str.substr(3, 5);
cout << subStr << endl; // 輸出”lo,Wo”
- 插入
方法 | 說明 |
---|---|
string& insert(pos, str); | 在pos之前插入str |
string& insert(pos, str, subPos, subLen); | pos之前插入字元串str從subPos開始的subLen字元 |
string& insert(pos, n, c); | 在pos之前插入n個字元c |
- 删除
方法 | 說明 |
---|---|
string& erase(pos, len); | 删除從pos開始的len個字元 |
iterator erase(const_iterator ite); | 删除ite處的一個字元,傳回删除後疊代器位置 |
iterator erase(const_iterator first, const_iterator last); | 删除first到last之間的字元 |
str.clear(); | 清空所有字元 |
- 交換
-
查找:#include
[1] find: 整體查找,傳回元素下标
// case1:
int position = s.find('a'); if (position != s.npos) {std::cout << "找到元素";}
// case2:
int position = s.find('b', 5); // 從下标5開始查找字元,找到傳回絕對下标
// 傳回值:找到傳回下标,沒找到傳回一個特殊标記 string::npos 或 s.npos (其值為-1)
注釋:string.find不光能找字元出現位置,也能找子串出現位置,舉例
sting s1 = "I'm Jack";
string s2 = "not ";
s1.insert(s1.find("Jack", 0), s2);
std::cout << s1 << std::endl; // I'm not Juruo
[2] find_first_of: 傳回字元或字元串第一次出現的位置
auto position = s.find_first_of('a');
void FindFirstOfTest()
{
string str = "0123456789abcdef";
int index = str.find_first_of("456");
std::cout << index << std::endl; // 輸出4
int newIndex = str.find_first_of("xy2");
std::cout << newIndex << std::endl; // 輸出2,x y都找不到,第一個找到的是2,對應下标2
}
[3] find_last_of: 傳回字元或字元串最後一次出現的位置
[4] find_first_not_of: 傳回第一個與子串不比對的字元的位置
void FindFirstOfTest()
{
string str="0123456789abcdef";
int index = str.find_first_not_of("013");
cout << index << endl; // 輸出:2,第一個與子串不比對的字元3對應下标2
int newIndex = str.find_first_not_of("0123456789abcdef");
cout << newIndex << endl; // 輸出:-1, 即string::npos,完全比對
}
[5] find_last_not_of: 傳回最後一個與子串不比對的字元的位置
[6] rfind: 完全比對,同find。反向查找子串最後出現的位置
[7] strstr: 能實作和find函數類似功能
- strstr參數有兩個:主字元串, 子字元串
- strstr的傳回值是指針:找到傳回首次出現位址,減去首位址得到索引;找不到傳回NULL
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
char a[] = "abcddabc";
char b[] = "dda";
int index;
string str1(a);
string str2(b);
int index = str1.find(str2);
if (index != string::npos) {
std::cout << "str1找到str2" << std::endl;
}
char* pos = strstr(a, b);
if (pos != null) {
std::cout << "%s" << pos << std::endl; // 輸出ddabc,首次出現位址輸出後得到ddabc
}
return 0;
}
參考文章:
string詳解
string方法
string的find方法
string介紹:推薦閱讀
created by shuaixio, 2021.07.28