#include <iostream>
#include <string>
using namespace std;
int main(void)
{
//构造应用
string str1("1:666");//string(const char* s);
string str2(str1); //string(const string& str);
string str3(6,'6'); //string(int n, char c);
cout << str1 << endl << str2 << endl << str3 << endl << endl;
//赋值应用(运算符重载 = ,函数assign)
//运算符重载
str1 = "2: 777"; //string& operator=(const char* s);
str2 = str1; //string& operator=(const string &s);
cout << str1 << endl << str2 << endl;
str1 = 'T'; //string& operator=(char c);
cout << str1 << endl;
//函数assign
str1.assign("3: 888888"); //string& assign(const char *s);
str2.assign(str1); //string& assign(const string &s);
cout << str1 << endl << str2 << endl;
str1.assign("3: 888888",6); //string& assign(const char *s, int n);
str2.assign(6, '8'); //string& assign(int n, char c);
cout << str1 << endl << str2 << endl << endl;
str1.assign("");
str3 = str2 = str1;
//字符串拼接(重载+= , 函数append)
str1 += "hellow "; //string& operator+=(const char* str);
str2.append("worl"); //string& append(const char *s);
str3.operator+=('d'); //string& operator+=(const char c);
str1 += str2 += str3; //string& operator+=(const string& str);
cout << str1 << endl;
str1.append(" !!!!!" , 6); //string& append(const char *s, int n); 把字符串s的前n个字符连接到当前字符串结尾
cout << str1 << endl;
str1.append(str1,15,2); //string& append(const string &s, int pos, int n);字符串s中从pos开始的n个字符连接到字符串结尾
cout << str1 << endl << endl;
//查找(函数find,rfind)和替换(replace)
cout << str1.find(str2) << endl; //int find(const string& str, int pos = 0) const;查找str第一次出现位置,从pos开始查找
str1.replace(15, 5, str3.assign("66666"));//string& replace(int pos, int n, const string& str);//替换从pos开始n个字符为字符串str
str2.replace(5,5," wall");
cout << str1 << endl << str2 << endl << endl;
//字符串比较(函数compare)相等为0
cout << !str1.compare(str2) << endl << endl; //int compare(const string &s) const;与字符串s比较
//字符存取(函数at , 重载[])
str3[1] = 'x'; //char& operator[](int n); n为字符的位置
str3.at(3) = 'x'; //char& at(int n);
cout << str3 << endl << endl;
//插入(函数insert)与删除(函数erase)
str1.insert(15,"Are you ok?");//string& insert(int pos, const char* s);
str2.insert(5," ");
str2.insert(6,4,'o'); //string& insert(int pos, int n, char c); 在指定位置插入n个字符c
cout << str1 << endl << str2 << endl;
str2.erase(6,2); //string& erase(int pos, int n = npos);删除从Pos开始的n个字符
cout << str2 << endl << endl;
//子串(函数substr)
cout << str2.substr(0, 5) << endl << endl;//string substr(int pos = 0, int n = npos) const; 返回由pos开始的n个字符组成的字符串
//获取字符串长度(函数size,length)
cout << str2.size() << " " << str2.length() << endl;
system("pause");
return 0;
}