# include<iostream>
using namespace std;
# include<string>
void test01()
{
string str1 = "abcdefgde";//从0开始索引,没有返回-1
int pos = str1.find("df");
cout << pos;
//rfind从右往左查找
//find从左往右查
pos=str1.rfind("de");
cout << pos;
}
void test02()
{
string str1 = "abcdefg";
str1.replace(1, 3, "11111");
cout << str1;
}
//字符串比较,按照ascll码值一个一个比较
void test03()
{
string str1 = "hello";
string str2 = "hello";
if (str1.compare(str2) == 0)
{
cout << "str1==str2";
}
}
//string的字符存取操作
void test04()
{
string str = "hello";
//通过中括号访问字符
for (int i = 0; i < str.size(); i++)
{
cout << str[i];
//通过at进行访问
cout << str.at(i) << endl;
}
}
//查入和删除字符串
void test05()
{
string str = "hello";
//插入
str.insert(1, "111");
cout << str << endl;
//在一号位置之前进行插入
//删除
str.erase(1, 3);
cout << str << endl;
}
//截取子串
void test06()
{
string str = "abcdef";
string sunStr = str.substr(1, 3);
cout << sunStr;
}
//实用操作
void test07()
{
string email = "zhangsan@sina.com";
//从邮件地址中,获取用户名信息
int pos=email.find('@');
string username = email.substr(0, (pos));
}
int main(void)
{
test01();
test02();
test02();
test03();
test04();
test05();
test06();
return 0;
}
string容器的内容(复习用)
最新推荐文章于 2024-09-11 11:53:23 发布