string容器的内容(复习用)

# 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lndksk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值