#include <iostream>
#include <string>
#include<vector>
using namespace std;
void test01()
{
string s;
cout << s << endl;
const char * str = "Hello Word";
string s1(str);
cout << s1 << endl;
string s2(s1);
cout << s2 << endl;
string s3(10, 'a');
cout << s3 << endl;
}
void test02()
{
string str1;
str1 = "Hello Word";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("Hello C++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("Hello C++",5);
cout << "str4 = " << str4 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(10,'a');
cout << "str7 = " << str7 << endl;
}
void test03()
{
string str1="我";
str1 += "爱玩";
cout << "str1 = " << str1 << endl;
str1 += ":";
cout << "str1 = " << str1 << endl;
string str3 = "英雄联盟";
str1 += str3;
cout << "str1 = " << str1 << endl;
str1.append("、PUBG");
cout << "str1 = " << str1 << endl;
str1.append("xxx", 2);
cout << "str1 = " << str1 << endl;
string str4="喵喵喵";
str1.append(str4);
cout << "str1 = " << str1 << endl;
string str5 = "abcdefg";
str1.append(str5, 3, 2);
cout << "str1 = " << str1 << endl;
}
void test04()
{
string str1("abcdefghijklmn");
int pos = str1.find("af");
cout << "下标位置为:" << pos << endl;
pos = str1.rfind("mn");
cout << "下标位置为:" << pos << endl;
str1.replace(1, 3, "卧槽");
cout << str1 << endl;
}
void test05()
{
string str1("Hello");
string str2("Hello");
cout << "比较结果为:" << str1.compare(str2) << endl;
cout << (str1 == str2) << endl;
}
void test06()
{
string str("Hello");
cout << str[1] << endl;
cout << str.at(2) << endl;
}
void test07()
{
string str("he");
str.insert(1, "a");
cout << str << endl;
str.insert(3, 4, 97);
cout << str << endl;
str.erase(3, 2);
cout << str << endl;
}
void test08()
{
string str("Hello Word");
string str1 = str.substr(0, 5);
cout << str1 << endl;
}