时间:2021-06-26 08:46:07 | 栏目:C代码 | 点击:次
原型为:unsigned int find(const basic_string &str) const;
作用:查找并返回str在本串中第一次出现的位置,位置从0开始
例子如下:
#include <iostream> using namespace std; int main() { string str = "i love china. china love me"; string find_str = "love"; cout << str.find(find_str); // 2 return 0; }
string append(const char* s) ;
#include <iostream> using namespace std; int main() { string str = "i love china. "; char append_str[] = "china love me"; cout << str.append(append_str) << endl; // i love china. china love me cout << str << endl; // i love china. china love me return 0; }
string & insert(unsigned int p0, const char * s);
#include <iostream> using namespace std; int main() { string str = "i love . china love me"; char insert_str[] = "china"; cout << str.insert(7, insert_str) << endl; // i love china. china love me cout << str << endl; // i love china. china love me return 0; }
string substr(unsigned int pos, unsigned int n) const;
#include <iostream> using namespace std; int main() { string str = "i love china. china love me"; cout << str.substr(2, 22) << endl; // love china. china love return 0; }
string & erase(unsigned int pos);
string & erase(unsigned int pos, unsigned int n);
#include <iostream> using namespace std; int main() { string str1 = "i love china. china love me"; cout << str1.erase(12) << endl; // i love china cout << str1 << endl; // i love china string str2 = "i love china. china love me"; cout << str2.erase(7, 18) << endl; // i love me cout << str2 << endl; // i love me return 0; }