c++string类的常用功能函数

😄1.resize
☀️2.append
🍰3.assign
🐶4.insert:
🐱5.erase
🦁6.replace
🦆7.find
🍓8.substr

😄1.resize

resize是重置空间的方法

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
void test1()
{
	string s1("abcdef");
	cout << s1.size() << endl;
	cout << s1 << endl;
	s1.resize(5,'x');
	cout << s1.size() << endl;
	cout << s1 << endl;

}
int main()
{
	test1();
	return 0;
}

在这里插入图片描述

☀️2.append

append方法是为string类对象添加字符串

void test2()
{
	string s1;
	s1.append("xxx");
	cout << s1 << endl;
	s1.append("abcd");
	cout << s1 << endl;
}
int main()
{
	test1();
	test2();
	return 0;
}

在这里插入图片描述

🍰3.assign

assign是替换字符串的功能。

void test3()
{
	string s1 = "abcdef";
	s1.assign("xx");
	cout << s1 << endl;
}

在这里插入图片描述

🐶4.insert

insert在字符串某个位置插入字符串。

void test4()
{
	string s1 = "abcdef";
	s1.insert(2,"qw");
	cout << s1 << endl;
}

在这里插入图片描述

🐱5.erase

erase是删除部分字符串。

void test5()
{
	string s1 = "abxdef";
	s1.erase(2);
	cout << s1 << endl;
}

在这里插入图片描述

🦁6.replace

replace是从某个位置开始替换若干长度字符串。

void test6()
{
	string s1 = "abcde";
	s1.replace(1,3, "qw");
	cout << s1 << endl;
}

在这里插入图片描述

🍓7.find

find是找到某个字符在字符串中第一个位置。

void test7()
{
	string s1 = "abcdbf";
	int i=s1.find('b',2);
	cout << i << endl;
}

在这里插入图片描述

🦆8.substr

substr是截取一部分字符串保存下来。

void test8()
{
	string s1 = "abcdefgh";
	string s2=s1.substr(2,5);
	cout << s2 << endl;
	
}

在这里插入图片描述

😄以上就是string类的基本常用方法,谢谢大家的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值