C++记录(6)

灰色的青

C++基础总结

string字符串

1.字符串求子串

string substr(int pos = 0,int n = npos) const; //返回pos 开始的n个字符组成的字符串
string str = "abcdefg";
string str_sub = str.substr(2,2);
cout << str_sub<<endl;

2.string 字符串类的查找

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 = 0,int n ); 
// **从pos开始查找字符串s中钱你个字符在当前串中的位置**
int find(const string &s,int pos = 0) const; //从pos开始查找字符串s在当前串中的位置

//查找成功时返回所在位置,是个 >=0 的数,失败返回 string::npos 的值。npos 的值默认就是  -1

3.string字符串类的替换操作

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 string &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, int n, char c);    //把 [first0,last0)之间的部分替换为n个字符c

4、string字符串类的插入操作:

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);
//同理

5、string字符串类的删除操作


iterator erase(iterator first, iterator last);    //删除 [first,last)之间的所有字符,返回删除后迭代器的位置

iterator erase(iterator it);    //删除it指向的字符,返回删除后迭代器的位置

string &erase(int pos = 0, int n = npos);    //删除pos开始的n个字符,返回修改后的字符串备注:相关代码演示详见视频教程!

猜你喜欢

转载自blog.csdn.net/qq_39750907/article/details/104206008