#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
void test1()
{
string str = "hello";
cout << str << endl;
//read
for(int i=0; i<str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
for(int i=0; i<str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
//write
str[0] = 'u';
cout << str << endl;
str.at(1) = 'u';
cout << str << endl;
}
void test2()
{
string str = "world";
str.insert(1, "222"); //在下标1位置插入"222"
cout << str << endl;
str.erase(1, 3); //从下标1位置开始删除其后3个字符
cout << str << endl;
}
void test3()
{
string str= "hi,c++";
string ss = str.substr(1, 3); //从下标1位置起截取其后3个字符的子串
cout << ss << endl;
string email= "eliauk@hftec.com";
int pos = email.find("@");
string username = email.substr(0, pos);
cout << username << endl;
}
int main()
{
/*
3.1.7 string字符存取
char& operator[](int n); //通过[]方式取字符
char& at(int n); //通过at方法获取字符
*/
test1();
/*
3.1.8 string插入和删除
string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c); //在指定位置插入n个字符c
string& erase(int pos, int n = npos); //删除从Pos开始的n个字符
*/
test2();
/*
3.1.9 string子串截取
string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
*/
test3();
system("pause");
return 0;
}