- 標准庫類型string表示可變長的字符序列,為了在程序中使用string類型,我們必須包含頭文件: #include <string>
- 聲明一個字符串
- 聲明一個字符串有很多種方式,具體如下:
1 string s;//調用默認構造函數,s為一個空字符串
2 string s(str);//等價於string s = str;調用拷貝構造函數,s是str的備份
3 string s(str,strindex);//將字符串str內始於strindex位置的部分當作s的初始值
4 eg.string str = "123456789"; 5 string s(str,3);//s的初值為str由位置3開始的字符串,即456789
6 string s(str,stridx,strlen); // 將字符串str由stridx位置起始且長度為strlen的部分最為s的初值,如果strlen大於最大長度,則只截取字符串最大長度
7 eg.string s(str,3,10);//s=456789,由位置3開始,截取長度為10的部分,由於str剩余部分長度小於10,則截取str剩余最大長度
8 string s(cstr);//將C風格字符串作為s的初值
9 eg.string s("hello");//s的初值為hello
10 string s(cstr,length);//將C風格字符串的length長度部分作為s的初值
11 eg.string s("hello",2);//s="he"
12 string s(num,c);//生成一個字符串,包含num個c字符
13 eg.string s(10,'c');//s的初值為“cccccccccc”
- 字符串操作函數
- c++字符串的操作函數很多,這里把常用的羅列出來
1 =、assign()//用於賦予新值,assign函數用於將一個字符串的部分內容賦值給另一個string對象
2 eg.string s1 = "hello"; 3 string s2; 4 s2.assign(s1,0,3);//s2的值為“hel”
5
6 swap() //交換兩個字符串的內容
7 eg.string s1 = "hello"; 8 string s2 = "world"; 9 swap(s1,s2);//swap函數將s1和s2的內容交換,現在s1="world",s2="hello"
10
11 +=、append()、push_back()//在字符串尾部追加內容,"+="可追加string對象,字符以及C風格字符串,append函數則可以追加string對象和C風格字符串,push_back函數則只能追加字符
12 eg.string s1 = "hello"; 13 string s2 = " world"; 14 s1 += s2;//正確,s1的值為”hello world“
15 s1 +="world";// 正確,s1的值為"hello world"
16 s1 +='c'; //正確,s1的值為"helloc"
17
18 s1.append(s2);//正確,s1的值為"hello world"
19 s1.append(" world");//正確,s1的值為"hello world"
20 s1.append('c');//錯誤,append函數不支持追加字符
21
22 s1.push_back(s2);//錯誤
23 s1.push_back("world");//錯誤
24 s1.push_back('c');//正確
25
26
27 insert()//用於插入字符串
28 eg.string s1 = "hello"; 29 s1.insert(0,"world ");//s1的值為world hello
30
31 erase()//用於刪除字符的
32 eg.string str("This is an example phrase."); 33 string::iterator it;//迭代器
34
35 str.erase(10,8);//str的值為"This is an phrase.",刪除了從位置10開始的8個字符
36
37 it = str.begin()+9;//迭代器位置為9
38 str.erase(it);//刪除了從it迭代器位置處的一個字符,str="This is a phrase."
39
40 str.erase(str.begin()+5,str.end()-7);//刪除兩個參數之間的所有字符,str="This phrase."
41
42
43 clear()函數和~string()//都是用來刪除全部字符的
44 eg.str.clear();//刪除str的全部字符,此時str為一個空串
45 str.~string();//銷毀所有字符,釋放內存
46
47 replace()函數,用於替換字符 48 eg.1.string line = "this@ is@ a test string!"; 49 line = line.replace(line.find("@"),1,"");//將line中從find的@位置開始替換一個長度的字符為"" 結果為this is@ a test string!
50
51 ==、!=、<、<=、>、>=、compare()//比較字符串
52 eg.string s1 = "haha"; 53 string s2 = "haha"; 54 if(s1.compare(s2) == 0){ 55 cout << "相等" << endl; 56 } 57
58 size()函數和length()函數,返回字符串的字符數 59 eg.string str = "haha"; 60 str.size() 等於 str.length(),值均為4 61
62 empty()//判斷字符串是否為空
63
64 下標法str[index]或者str.at(index)獲取字符串內指定位置的字符 65
66 data()函數,將內容以字符數組的形式返回
- C++字符串和C字符串的轉換
- C++提供的由C++字符串得到對應的C_string的方法是使用data()、c_str()和copy(),其中,data()以字符數組的形式返回字符串內容,但並不添加'\0'.
- c_str()返回一個以'\0'結尾的字符數組
- copy()則把字符串的內容復制或寫入已有的c_string或字符數組內
- 元素存取
- 我們可以使用下標操作符[]和函數at()來對字符串的字符進行訪問,但是應該注意的是下標操作符並不會檢查索引是否有效,如果索引失效,會引起未定義的行為
- at()函數則會檢查索引,如果索引失效會拋出out_of_range異常
- 注意,操作符可取到字符串尾部的'\0'字符
已知類string的原型為:
1 class String 2 { 3 public: 4 String(const char *str = NULL);//普通構造函數
5 String(const String &other);//拷貝構造函數
6 ~String(void);//析構函數
7
8 private: 9 char *m_data;//用於保存字符串
10 };
編寫上述三個函數的實現:
1 //普通構造函數 2 String:String(const char *str) 3 { 4 if(str == NULL){ 5 m_data = new char[1]; 6 *m_data = '\0'; 7 }else{ 8 int length = strlen(str); 9 m_data = new char[length+1]; 10 strcpy(m_data,str); 11 } 12 } 13 14 //析構函數 15 String::~String(void) 16 { 17 delete []m_data; 18 } 19 20 //拷貝構造函數 21 String::String(const String &other) 22 { 23 int length = strlen(other.m_data); 24 m_data = new char[length+1]; 25 strcpy(m_data,other.m_data); 26 }