**string**是**STL**中一個常用的標準庫類型。**string**在STL中表示可變長的字符序列,它包含在頭文件**#include <string>**中。
具體關于**string**類的介紹可以看這:[**https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx**](https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx)
下面來看看一些常用的方法:
**1、string的初始化**
~~~
string s1;//默認構造函數,s1為空串
string s2(s1);//將s2初始化為s1的一個副本
string s3("value");//將s3初始化一個字符串面值副本
string s4(n, 'c');//將s4初始化為字符'c'的n個副本
cin>>s5;//讀取有效字符到遇到空格
getline(cin, s6);//讀取字符到遇到換行,空格可讀入,直到‘\n’結束
getline(cin, s7, 'a');//抑制讀取,直到遇到‘a’結束,其中任何字符包括'\n'都能夠讀入
~~~
**sring**的初始化如上,不再贅述。
**2、string::append()**
~~~
string &append(const char *s); //把c類型字符串s連接到當前字符串結尾
string &append(const char *s,int n); //把c類型字符串s的前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);//把迭代器first和last之間的部分連接到當前字符串的結尾
~~~
下面是一些舉例:
~~~
string s("abc");
string t("123");//或char *t = "123";
s.append(t);//將t加到s的尾部
s.append(s.begin() + 1, s.end());//將s的第[begin+1, end)加到s末尾
~~~
該方法用于添加一串字符到字符串的末尾。
**3、string::assign()**
~~~
string &assign(const char *s);//用c類型字符串s賦值
string &assign(const char *s,int n);//用c字符串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_itertor last);//把first和last迭代器之間的部分賦給字符串
~~~
下面是一些舉例:
~~~
string s("abc");
string t("123");//或char *t = "123";
s.assign(t);
~~~
賦值語句,**s**中的字符串被**t**中的字符串覆蓋,但**s.capacity()**不改變。
**4、string::at()**
~~~
cout<<s[1]<<endl;
cout<<s.at(1)<<endl;//同s[1],輸出字符串s中第2個字符
~~~
**5、string::front()、string::back()、string::begin()、string::cbegin()、string::end()、string::cend()、string::crbegin()、string::crend()、string::rbegin()、string::rend()、string::size()、string::capacity()、string::length()、string::reserve()、string::resize()、string::swap()**
~~~
int capacity()const; //返回當前容量(即string中不必增加內存即可存放的元素個數)
int max_size()const; //返回string對象中可存放的最大字符串的長度
int size()const; //返回當前字符串的大小
int length()const; //返回當前字符串的長度
bool empty()const; //當前字符串是否為空
void resize(int len,char c);//把字符串當前大小置為len,并用字符c填充不足的部分
~~~
其他就不說了。
**6、string::empty()**
~~~
s.empty();//返回ture表示空,false表示非空
~~~
返回值是一個**bool**量。
**7、string::compare()**
~~~
bool operator==(const string &s1,const string &s2)const;//比較兩個字符串是否相等
//運算符">","<",">=","<=","!="均被重載用于字符串的比較;
int compare(const string &s) const;//比較當前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比較當前字符串從pos開始的n個字符組成的字符串與s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比較當前字符串從pos開始的n個字符組成的字符串與s中pos2開始的n2個字符組成的字符串的大小
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
~~~
下面是一些舉例:
~~~
s.assign("123");
t.assign("234");
cout<<s.compare(t)<<endl;//輸出-1
cout<<s.compare(s)<<endl;//輸出0
cout<<t.compare(s)<<endl;//輸出1
~~~
操作串小于參數串,則返回**-1**;相等,則返回**0**;操作串大于參數串,則返回**1**。
**8、string::clear()**
~~~
s.clear();
~~~
**s.size()**為**0**,**s.capacity()**不變。
**9、string::find()、string::rfind()、string::find_first_of()、string::find_first_not_of()、string::find_last_of()**
~~~
int find(char c, int pos = 0) const;//從pos開始查找字符c在當前字符串的位置
int find(const char *s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
int find(const char *s, int pos, int n) const;//從pos開始查找字符串s中前n個字符在當前串中的位置
int find(const string &s, int pos = 0) const;//從pos開始查找字符串s在當前串中的位置
//查找成功時返回所在位置,失敗返回string::npos的值
int rfind(char c, int pos = npos) const;//從pos開始從后向前查找字符c在當前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//從pos開始從后向前查找字符串s中前n個字符組成的字符串在當前串中的位置,成功返回所在位置,失敗時返回string::npos的值
int find_first_of(char c, int pos = 0) const;//從pos開始查找字符c第一次出現的位置
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;
//從pos開始查找當前串中第一個在s的前n個字符組成的數組里的字符的位置。查找失敗返回string::npos
int find_first_not_of(char c, int pos = 0) const;
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;
//從當前串中查找第一個不在串s中的字符出現的位置,失敗返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) 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;
//find_last_of和find_last_not_of與find_first_of和find_first_not_of相似,只不過是從后向前查找
~~~
**string**的**find**非常豐富,要使用得當。
**10、string::insert()**
~~~
string &insert(int p0, const char *s);//在p0位置摻入字符串s
string &insert(int p0, const char *s, int n);//在p0位置摻入字符串s的前n個字符
string &insert(int p0,const string &s);//在p0位置摻入字符串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);//在it處插入[first,last)之間的字符
void insert(iterator it, int n, char c);//在it處插入n個字符c
~~~
下面是一些舉例:
~~~
s.insert(2, "www");//在2位置插入字符串"www"
s.insert(s.begin(), s.begin(), s.begin() + 4);//在s.begin位置插入s的[begin,begin+4)之間的字符
s.insert(2, "98-+=_76", 1, 3);//在2位置插入"98-+=_76"的[1,3]位置的字符
s.insert(1, "12345678", 5);//在1位置將"12345678"的前5個字符插入
s.insert(1, 6, '@');//在1位置連續6次插入字符‘@’
s.insert(s.begin()+2, 3, '#');//在begin+2位置連續3次插入字符‘#’
~~~
**string**的**insert**也是非常豐富的,要使用得當。
**11、string::erase()**
~~~
terator erase(iterator first, iterator last);//刪除[first,last)之間的所有字符,返回刪除后迭代器的位置
iterator erase(iterator it);//刪除it指向的字符,返回刪除后迭代器的位置
string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字符,返回修改后的字符串
~~~
下面是一些舉例:
~~~
s.erase(s.begin() + 2, s.begin() + 5);//刪除[begin+2,begin+5)之間的元素
s.erase(0, 4);//刪除[0,4)之間的元素
s.erase(s.begin() + 3);//刪除第begin+3位置的元素
s.erase(3);//刪除位置3及其以后的元素
~~~
**12、string::repalce()**
~~~
string &replace(int p0, int n0,const char *s);//刪除從p0開始的n0個字符,然后在p0處插入串s
string &replace(int p0, int n0,const char *s, int n);//刪除p0開始的n0個字符,然后在p0處插入字符串s的前n個字符
string &replace(int p0, int n0,const string &s);//刪除從p0開始的n0個字符,然后在p0處插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//刪除p0開始的n0個字符,然后在p0處插入串s中從pos開始的n個字符
string &replace(int p0, int n0,int n, char c);//刪除p0開始的n0個字符,然后在p0處插入n個字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之間的部分替換為字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之間的部分替換為s的前n個字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之間的部分替換為串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之間的部分替換為n個字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之間的部分替換成[first,last)之間的字符串
~~~
**string**的替換。
**13、string::substr()**
~~~
string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字符組成的字符串
~~~
比如:
~~~
s.substr(3, 8);//截取s的[3, 3+8)字符串
~~~
**14、string::copy()**
~~~
int copy(char *s, int n, int pos = 0) const;//把當前串中以pos開始的n個字符拷貝到以s為起始位置的字符數組中,返回實際拷貝的數目
~~~
**15、string::data()、string::c_str()**
~~~
const char *data()const;//返回一個非null終止的c字符數組
const char *c_str()const;//返回一個以null終止的c字符串
~~~
總之,**string**是一個強大的字符處理類型庫,要使用得當。
部分**參考**并**引用**自:
**1、C++STL之string:[http://blog.csdn.net/y990041769/article/details/8763366](http://blog.csdn.net/y990041769/article/details/8763366)**
**2、標準C++中的string類的用法總結:[http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html](http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html)**