string是STL中一个常用的标准库类型。string在STL中表示可变长的字符序列,它包含在头文件#include <string>中。
具体关于string类的介绍可以看这:https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx
下面来看看一些常用的方法:
1、string的初始化
- string s1;
- string s2(s1);
- string s3("value");
- string s4(n, 'c');
- cin>>s5;
- getline(cin, s6);
- getline(cin, s7, 'a');
sring的初始化如上,不再赘述。
2、string::append()
- string &append(const char *s);
- string &append(const char *s,int n);
- string &append(const string &s);
- string &append(const string &s,int pos,int n);
- string &append(int n,char c);
- string &append(const_iterator first,const_iterator last);
下面是一些举例:
- string s("abc");
- string t("123");
- s.append(t);
- s.append(s.begin() + 1, s.end());
该方法用于添加一串字符到字符串的末尾。
3、string::assign()
- string &assign(const char *s);
- string &assign(const char *s,int n);
- string &assign(const string &s);
- string &assign(int n,char c);
- string &assign(const string &s,int start,int n);
- string &assign(const_iterator first,const_itertor last);
下面是一些举例:
- string s("abc");
- string t("123");
- s.assign(t);
赋值语句,s中的字符串被t中的字符串覆盖,但s.capacity()不改变。
4、string::at()
- cout<<s[1]<<endl;
- cout<<s.at(1)<<endl;
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;
- int max_size()const;
- int size()const;
- int length()const;
- bool empty()const;
- void resize(int len,char c);
其他就不说了。
6、string::empty()
返回值是一个bool量。
7、string::compare()
- bool operator==(const string &s1,const string &s2)const;
-
- int compare(const string &s) const;
- int compare(int pos, int n,const string &s)const;
- int compare(int pos, int n,const string &s,int pos2,int n2)const;
- 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;
-
下面是一些举例:
- s.assign("123");
- t.assign("234");
- cout<<s.compare(t)<<endl;
- cout<<s.compare(s)<<endl;
- cout<<t.compare(s)<<endl;
操作串小于参数串,则返回-1;相等,则返回0;操作串大于参数串,则返回1。
8、string::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;
- int find(const char *s, int pos = 0) const;
- int find(const char *s, int pos, int n) const;
- int find(const string &s, int pos = 0) const;
-
- int rfind(char c, int pos = npos) const;
- 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;
-
- int find_first_of(char c, int pos = 0) const;
- 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;
-
- 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;
-
- 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;
-
string的find非常丰富,要使用得当。
10、string::insert()
- string &insert(int p0, const char *s);
- string &insert(int p0, const char *s, int n);
- string &insert(int p0,const string &s);
- string &insert(int p0,const string &s, int pos, int n);
-
- string &insert(int p0, int n, char c);
- iterator insert(iterator it, char c);
- void insert(iterator it, const_iterator first, const_iterator last);
- void insert(iterator it, int n, char c);
下面是一些举例:
- s.insert(2, "www");
- s.insert(s.begin(), s.begin(), s.begin() + 4);
- s.insert(2, "98-+=_76", 1, 3);
- s.insert(1, "12345678", 5);
- s.insert(1, 6, '@');
- s.insert(s.begin()+2, 3, '#');
string的insert也是非常丰富的,要使用得当。
11、string::erase()
- terator erase(iterator first, iterator last);
- iterator erase(iterator it);
- string &erase(int pos = 0, int n = npos);
下面是一些举例:
- s.erase(s.begin() + 2, s.begin() + 5);
- s.erase(0, 4);
- s.erase(s.begin() + 3);
- s.erase(3);
12、string::repalce()
- string &replace(int p0, int n0,const char *s);
- string &replace(int p0, int n0,const char *s, int n);
- string &replace(int p0, int n0,const string &s);
- string &replace(int p0, int n0,const string &s, int pos, int n);
- string &replace(int p0, int n0,int n, char c);
- string &replace(iterator first0, iterator last0,const char *s);
- string &replace(iterator first0, iterator last0,const char *s, int n);
- string &replace(iterator first0, iterator last0,const string &s);
- string &replace(iterator first0, iterator last0,int n, char c);
- string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);
string的替换。
13、string::substr()
- string substr(int pos = 0,int n = npos) const;
比如:
14、string::copy()
- int copy(char *s, int n, int pos = 0) const;
15、string::data()、string::c_str()
- const char *data()const;
- const char *c_str()const;
总之,string是一个强大的字符处理类型库,要使用得当。
部分参考并引用自:
1、C++STL之string:http://blog.csdn.net/y990041769/article/details/8763366
2、标准C++中的string类的用法总结:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html