C++中string類的方法總結

string類與C語言中的以 '/0' 結尾的字符串不同, string類的本質上是以字符作爲元素的vector特化版本;不存在0字符結尾這個概念,能裝入'\0'這種數據

本文介紹string類的常用方法,方便日後複習:

1.常用構造函數

string s1;//定義字符串,調用默認構造函數,生成空字符串 s1 = "";

string s2("abcde"); //定義字符串,並調用string(char *s)構造函數來進行初始化,生成s2 = "abcde"字符串

string s3(3,'a');//定義字符串s3,並調用string(int n,char c)構造函數來進行初始化,生成由 n個字符'c'組成的字符串,比如 s3 = "aaa";通常用來爲已知大小的字符串分配空間

string s4 = "rtrt";//定義字符串s4,並調用拷貝構造函數string(string &s),生成字符串s4 = "rtrt"

2.獲取字符串的長度

string s = "abcdef";

//string::length() 和 string::size()兩者作用相同,返回字符串中包含的字符數
s.length();  //字符串所含字符數爲6
s.size();   //字符串所含字符數爲6

s.empty();//判斷字符串s是否爲空,爲空則返回true,不爲空則返回false

3.單個字符和字符串的拼接

//第一種方法是通過重載 + 操作符
//即通過 + 既可以連接string對象,又可以連接C語言中的字符串 

//string &operator+=(const string &s);  //把字符串s連接到當前字符串結尾
string s1 = "abc";
string s2 = "def";
string s3 = s1 +s2; //輸出s3 = "abcdef"


//string &operator+=(const char *s);//把字符串s連接到當前字符串結尾
char *s4 = "ghi";
string s5 = s3 + s4; //輸出s5 = "abcdefghi" 



//第二種方法是通過string類的append方法
string &append(const char *s);    //把字符串s連接到當前字符串結尾

string a1 = "abc";
char* a2 = "def";
a2.append(a1); //輸出a2爲"abcdef"

string &append(const string &s);   //同operator+=()

string &append(int n, char c);   //在當前字符串結尾添加n個字符c

char a3 = 'g';
a2.append(3,g);//輸出a2爲"abcdefggg"

4.單個字符和字符串的插入

//在pos位置插入字符串s,字符串有兩種表示形式,char*型和string型
string &insert(int pos, const char *s);
string &insert(int pos, const string &s);


string s1 = "abcdef";
string s2 = "yy";
s1.insert(2,s2);//在s1的下標爲2的位置處插入字符串s2;輸出結果爲"abyycdef"


string &insert(int pos, int n, char c);  //在pos位置 插入n個字符c

string a1 = "abc";
char a2 = 'y';
a1.insert(0,3,a2);在a1中下標爲0的地方插入3個字符a2,輸出結果爲a1 = "yyyabc"

5.獲取字符串的子串

//返回由pos開始的n個字符組成的子字符串
string substr(int pos=0, int n=npos) const;//包含下標pos處的字符,總字符爲npos的子串    

string s1 = "abcdefghi";
string s2 = s1.substr(1,3);//獲取從1開始,總字符個數爲3的子串
//輸出結果爲  s2 = "bcd"

6.字符串的查找

//在字符串中查找特定的字符或字符串
int find(char c,int pos=0) const;  //從pos開始查找字符c在當前字符串的位置 
int find(const char *s, int pos=0) const;  //從pos開始查找字符串s在當前字符串的位置
int find(const string &s, int pos=0) const;  //從pos開始查找字符串s在當前字符串中的位置
find函數如果查找不到,就返回 string::npos ;找到則返回首字符的下標

string s1 = "abcdefg";
s1.find('d',1);//從下標1處開始找字符d
//查找結果爲3,表示在下標3處找到字符d

s1.find("ef",0);//從下標0處開始查找字符串"ef",返回結果爲4,表示在下標4處找到字符串"ef"

s1.find("yy",0);//此時返回結果爲string::npos

7.字符串的替換

string &replace(int pos, int n, const char *s);//刪除從pos開始的n個字符,然後在pos處插入串s
string &replace(int pos, int n, const string &s);  //刪除從pos開始的n個字符,然後在pos處插入串s
string s1 = "abcdef";
s1.replace(1,3,"yy");//將下標1-3處的字符串替換爲"yy"
//輸出結果爲 "ayyef"

8.字符串子串或單字符的刪除

iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除後迭代器的位置,注意右邊爲開區間
iterator erase(iterator it);//刪除it指向的字符,返回刪除後迭代器的位置
string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字符,返回修改後的字符串
string s1 = "abcdefgh";
s1.erase(s1.begin(),s1.begin()+2);//刪除"ab"
//輸出結果爲"cdefgh"

string s2 = "abcdefg";
s2.erase(s2.begin());//刪除字符'a'
//輸出結果爲"bcdefg"

string s3 = "abcdef";
s3.erase(2,3);//刪除s3下標從2開始的3個字符,即"cde"
//輸出結果爲"abf"

9.字符串的比較

int compare(const string &s) const;  //與字符串s比較
int compare(const char *s) const;   //與字符串s比較
//compare函數在>時返回 1,<時返回 -1,==時返回 0。比較區分大小寫,比較時參考字典順序,排越前面的越小。大寫的A比小寫的a小。

10.string類的輸入輸出操作

string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作

string s1 = "abcdef";
cout << s1 << endl; //輸出s1 = "abcdef"

string s2;
cin >> s2;//在顯示串口從鍵盤鍵入"yyy"
cout << s2 <<endl;//輸出s2 = "yyy"

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章