- string對象的初始化
- string的賦值和連接
- 比較string
- 子串
- 交換string
- string的特性
- 尋找string中的字符
- 替換string中的字符
- 在string中插入字符
- 轉換成C語言式
char *
字符串
string類
string類是一個模板類,它的定義如下:
typedef basic_string<char> string;
使用string類要包含頭文件<string>
string對象的初始化
- 正確的初始化方法
string s1("Hello"); // 一個參數的構造函數
string s2(8, ‘x’); // 兩個參數,s2中有8個x
string month = “March”;
- 不提供以字符和整數爲參數的構造函數
錯誤的初始化方法:
string error1 = ‘c’; // 錯
string error2(‘u’); // 錯
string error3 = 22; // 錯
string error4(8); // 錯
- 可以將字符型賦值給string對象
string s;
s = ‘n’;
- 構造的string太長而無法表達 -> 會拋出
length_error
異常 - string對象的長度用成員函數
length()
讀取
string s("hello");
cout << s.length() << endl;
- string支持流讀取運算符
string stringObject;
cin >> stringObject;
- string支持
getline
函數
string s;
getline(cin, s);
樣例程序
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1("Hello");
cout << s1 << endl;
string s2(8, 'x');
cout << s2 << endl;
string month = "March";
cout << month << endl;
string s;
s='n';
cout << s << endl;
return 0;
}
程序輸出:
Hello
xxxxxxxx
March
n
string的賦值和連接
- 用
=
賦值
string s1("cat"), s2;
s2 = s1;
- 用
assign
成員函數複製
string s1("cat"), s3;
s3.assign(s1);
- 用
assign
成員函數部分複製
string s1("catpig"), s3;
s3.assign(s1, 1, 3); //從s1中下標爲1的字符開始複製3個字符給s3 s3 = "atp"
- 單個字符複製
s2[5] = s1[3] = ‘a’;
- 逐個訪問string對象中的字符
string s1("Hello");
for(int i = 0; i< s1.length(); i++) {
cout << s1.at(i) << endl;
cout << s1[i] << endl;
}
成員函數at
會做範圍檢查, 如果超出範圍, 會拋出out_of_range
異常, 而下標運算符[]
不做範圍檢查
- 用
+
運算符連接字符串
string s1("good "), s2("morning! ");
s1 += s2;
cout << s1; // "good morning! "
- 用成員函數
append
連接字符串
string s1("good "), s2("morning! ");
s1.append(s2);
cout << s1;
s2.append(s1, 3, s1.size()); //s1.size(), s1字符數
cout << s2;
//下標爲3開始, s1.size()個字符
//如果字符串內沒有足夠字符, 則複製到字符串最後一個字符
比較string
- 用關係運算符比較string的大小
== , >, >=, <, <=, !=
- 返回值都是
bool
類型,成立返回true
,否則返回false
string s1("hello"), s2("hello"), s3("hell");
bool b = (s1 == s2);
cout << b << endl;
b = (s1 == s3);
cout << b << endl;
b = (s1 > s3);
cout << b << endl;
輸出:
1
0
1
- 用成員函數
compare
比較string的大小
string s1("hello"), s2("hello"), s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1, 2, s3, 0, 3); //s1 1-2; s3 0-3
int f5 = s1.compare(0, s1.size(), s3); //s1 0-end
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl;
輸出:
0 // hello == hello
1 // hello > hell
-1 // hell < hello
-3 // el < hell
1 // hello > hell
子串
- 成員函數
substr()
string s1("hello world"), s2;
s2 = s1.substr(4,5);
cout << s2 << endl; // o wor
交換string
- 成員函數
swap()
string s1("hello world"), s2("really");
s1.swap(s2);
cout << s1 << endl; // really
cout << s2 << endl; // hello world
string的特性
- 成員函數
capasity()
返回無需增加內存即可存放的字符數 - 成員函數
maximun_size()
返回string對象可存放的最大字符數 - 成員函數
length()
和size()
相同,返回字符串的大小/長度 - 成員函數
empty()
返回string對象是否爲空 - 成員函數
resize()
改變string對象的長度
string s1("hello world");
cout << s1.capacity() << endl;
cout << s1.max_size() << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1.empty() << endl;
cout << s1 << "aaa" << endl;
s1.resize(s1.length()+10);
cout << s1.capacity() << endl;
cout << s1.max_size() << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1 << "aaa" << endl;
s1.resize(0);
cout << s1.empty() << endl;
輸出(不同編譯器上可能會不一樣):
22 // capacity
18446744073709551599 // maximum_size
11 / length
11 // size
0 // empty
hello worldaaa // string itself and "aaa"
22
18446744073709551599
21
21
hello worldaaa
1
尋找string中的字符
- 成員函數
find()
string s1("hello world");
s1.find("lo"); // 3
//在s1中從前向後查找 “lo” 第一次出現的地方
//如果找到, 返回 “lo”開始的位置, 即 l 所在的位置下標
//如果找不到, 返回 string::npos (string中定義的靜態常量)
string s1("hello worlld");
cout << s1.find("ll", 1) << endl;
cout << s1.find("ll", 2) << endl;
cout << s1.find("ll", 3) << endl;
//分別從下標1, 2, 3開始查找 “ll”
- 成員函數
rfind()
string s1("hello world");
s1.rfind("lo"); // 3
//在s1中從後向前查找 “lo” 第一次出現的地方
//如果找到, 返回 “lo”開始的位置, 即 l 所在的位置下標
//如果找不到, 返回 string::npos
- 成員函數
find_first_of()
string s1("hello world");
s1.find_first_of("abcd"); // 10
//在s1中從前向後查找 “abcd” 中任何一個字符第一次出現的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
- 成員函數
find_last_of()
string s1("hello world");
s1.find_last_of(“abcd");
//在s1中查找 “abcd” 中任何一個字符最後一次出現的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
- 成員函數
find_first_not_of()
string s1("hello world");
s1.find_first_not_of("abcd"); // 0
//在s1中從前向後查找不在 “abcd” 中的字母第一次出現的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
- 成員函數
find_last_not_of()
string s1("hello world");
s1.find_last_not_of("abcd"); // 9
//在s1中從後向前查找不在 “abcd” 中的字母第一次出現的地方
//如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
string s1("hello worlld");
cout << s1.find("ll") << endl; // 2
cout << s1.find("abc") << endl; // 18446744073709551615
cout << s1.rfind("ll") << endl; // 9
cout << s1.rfind("abc") << endl; // 18446744073709551615
cout << s1.find_first_of("abcde") << endl; // 1
cout << s1.find_first_of("abc") << endl; // 18446744073709551615
cout << s1.find_last_of("abcde") << endl; // 11
cout << s1.find_last_of("abc") << endl; // 18446744073709551615
cout << s1.find_first_not_of("abcde") << endl; // 0
cout << s1.find_first_not_of("hello world") << endl; // 18446744073709551615
cout << s1.find_last_not_of("abcde") << endl; 10
cout << s1.find_last_not_of("hello world") << endl; //18446744073709551615
替換string中的字符
- 成員函數
erase()
string s1("hello worlld");
s1.erase(5); // 去掉下標 5 及之後的字符
cout << s1;
cout << s1.length();
cout << s1.size();
輸出:
hello55
- 成員函數
find()
string s1("hello worlld");
cout << s1.find("ll", 1) << endl;
cout << s1.find("ll", 2) << endl;
cout << s1.find("ll", 3) << endl;
//分別從下標1, 2, 3開始查找 “ll”
- 成員函數
replace()
string s1("hello world");
s1.replace(2, 3, “haha"); //將s1中下標2 開始的3個字符換成 “haha”
cout << s1;
輸出:
hehaha world
string s1("hello world");
s1.replace(2, 3, "haha", 1,2);
cout << s1;
//將s1中下標2 開始的3個字符
//換成 “haha” 中下標1開始的2個字符
輸出:
heah world
在string中插入字符
- 成員函數
insert()
string s1(“hello world”);
string s2(“show insert”);
s1.insert(5, s2); // 將s2插入s1下標5的位置
cout << s1 << endl;
s1.insert(2, s2, 5, 3); //將s2中下標5開始的3個字符插入s1下標2的位置
cout << s1 << endl;
輸出:
helloshow insert world
heinslloshow insert world
轉換成C語言式`char *`字符串
- 成員函數
c_str()
string s1("hello world");
printf("%s\n", s1.c_str());
// s1.c_str() 返回傳統的const char * 類型字符串
//且該字符串以 ‘\0’ 結尾
輸出:
hello world
- 成員函數
data()
string s1("hello world");
const char * p1 = s1.data();
for(int i=0; i < s1.length(); i++)
printf("%c",*(p1+i));
//s1.data() 返回一個char * 類型的字符串
//對s1 的修改可能會使p1出錯。
輸出:
hello world
- 成員函數
copy()
string s1("hello world");
int len = s1.length();
char * p2 = new char[len+1];
s1.copy(p2, 5, 0);
p2[5] = '\0';
cout << p2 << endl;
// s1.copy(p2, 5, 0) 從s1的下標0的字符開始,
// 製作一個最長5個字符長度的字符串副本並將其賦值給p2
// 返回值表明實際複製字符串的長度