关于string容器一些简单函数的讲解

一下来介绍一下string容器函数的用法:
这里是一些增加和删除的函数

#include<iostream>
using namespace std;
void test()
{
	string str = "I";
	str += " LOVE LOL";
	str.insert(5,4,'a');//c语言字符串,在第五个位置,插入四个a
	cout << "str1= " << str << endl;//
	str.insert(5, "aaa");//c++风格字符串,在第五个位置开始插入三个a
	cout << "str2= " << str << endl;
	str.erase(5, 7);//从第五个位置开始删除7个字符
	cout << "str3= " << str << endl;
	str.insert(5, str);//可以把定义好的变量名放入,运行时,插入的代码就是str,第五个位置插入str
	cout << "str4= " << str<<endl;
	str.erase(5, 10);//擦除第五个到第十个
	str.insert(0, "aaaaatforever",6, 7);//从aaaaatforever下标为6的位置找七个字符插入str当中去
	cout << "str5= " << str << endl;
	str=str.substr(0, 7);//取第0位到7位
	cout << "str6= " << str << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

结果如下:
在这里插入图片描述

对于上面的" "当中的字符串,可以替换成字符串变量,但是' '却不可以替换成字符串变量。
以下是一些获取字符的函数

#include<iostream>
using namespace std;
void test()
{
	string str = "I ";
	str += "LOVE LOL LOL";
	for (int i = 1; i < str.size(); i++)//通过size函数获得长度,通过[]的方式来获得(访问)字符
	{
		cout << str[i] << " ";
	}
	cout << endl;
	for (int i = 1; i < str.size(); i++)//通过size函数获得长度,通过at函数的方式来获得(访问)字符
	{
		cout << str.at(i) << " ";
	}
	cout << endl;
	str[0] = 'a';//也可通过[]进行修改,但是只能修改单个字符
	cout << str << endl;
	str.at(1) = 'b';//也可通过at函数进行修改,但是只能修改单个字符
	cout << str << endl;
	
}
int main()
{
	test();
	system("pause");
	return 0;
}

结果如下:
在这里插入图片描述
以下是assign函数的用法:

#include<iostream>
using namespace std;
#include<iostream>		
void test()
{
	string str1 = "hello world!";
	cout << str1 << endl;
	string str2(str1);
	cout << str2 << endl;
	string str3;
	str3 = str2;
	cout << str3 << endl;
	string str4;
	str4.assign("hello c++");
	cout << str4 << endl;
	string str5;
	str5.assign("hello c++", 5);//输出为hello
	cout << str5 << endl;
	string str6;
	str6.assign(str5);
	cout << str6 << endl;
	string str7;
	str7.assign(10, 'a');
	cout << str7 << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

结果如下:
在这里插入图片描述
assign函数主要的作用是赋值,其用法和作用相当简单
以下是append函数的用法:

#include<iostream>
using namespace std;
void test()
{
	string str = "我";
	str += "爱玩游戏";
	cout << "str=" << str << endl;
	string str2 = "loll";
	str += str2;
	cout << "str=" << str << endl;
	string str3 = "I ";
	str3.append("love ");
	//str3 += str2;
	//str3.append(str2);
	//cout << str3 << endl;
	str3.append("lol bbbb cccc", 3);
	cout << str3 << endl;
	//string str4;
	//tr3.append("aaa bbbb cccc", 3);
	str3.append(str2, 0, 3);//参数2,从哪个位置开始截至,参数3,截取几个位数
	cout << str3 << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}

结果如下:
在这里插入图片描述
append函数主要是起添加作用

总结:无论是添加字符还是删除字符还是其他函数,参数添加还是计算字符位置,下标都是从0开始索引,切记!切记!切记!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

潜渊@龙

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

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

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

打赏作者

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

抵扣说明:

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

余额充值