Day06 C++STL入门基础知识三——String容器(下)比较-存取-插入-删除-子串获取【全面深度剖析+例题代码展示】

永远相信,美好的事情即将发生!

1. 比较操作

1.1 比较方式

  • 比较字符串的ASCII码进行对比
  • 左=右 返回0
  • 左>右 返回1
  • 左<右 返回-1

1.2 函数原型

  • int compare(const string &s) const; 与字符串s比较
  • int compare(const char*s) const; 与字符串s比较

1.3 代码展示

  1. 左=右时
    在这里插入图片描述

  2. 左>右时
    在这里插入图片描述

  3. 左<右时
    在这里插入图片描述

  4. 代码

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
void text01() {
	//字符串的比较(类似于C里的strcmp()函数)
	string s1 = "hello";
	string s2 = "helpo";
	if (s1.compare(s2) == 0) {
		cout << "s1=s2" << endl;
	}
	else if (s1.compare(s2) > 0) {
		cout << "s1>s2" << endl;
	}
	else {
		cout << "s1<s2" << endl;
	}
}
int main() {
	text01();

	return 0;
}

2. 字符读写

2.1 字符读入/访问

2.1.1 方式

  • 通过[]下标访问
  • 通过.at()访问

2.1.2 代码展示

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
//字符串的存取
//访问修改单个字符
void text01() {
	//法①:通过 [] 访问单个字符
	string s1 = "hello";
	int len = s1.size();
	for (int i = 0; i < len; i++) {
		cout << s1[i] << " ";
	}
	cout << endl;

	//法②:通过at方式访问单个字符
	for (int i = 0; i < len; i++) {
		cout << s1.at(i) << " ";
	}
	cout << endl;
}
int main() {
	text01();
	return 0;
}

在这里插入图片描述

2.2 修改字符

2.2.1 方式

  • 使用下标[]直接修改
  • 使用.at()进行修改

2.2.2 代码展示

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
//字符串的存取
//修改单个字符
void text02() {
	//法①:通过[]来修改
	string s1 = "hello";
	s1[0] = 'p';
	cout << s1 << endl;
	
	//法②:通过s1.at()来修改
	s1.at(1) = 'q';
	cout << s1 << endl;
}
int main() {
	//text01();
	text02();
	return 0;
}

在这里插入图片描述

3. 插入和删除

3.1 函数原型

  • 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个字符

3.2 代码展示

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
//插入操作
void text01() {
	string s1 = "hello";
	s1.insert(1, "123");
	//从下标为1开始,插入"123"
	cout << s1 << endl;
}
void text02() {
	string s1 = "h123ello";
	s1.erase(1, 3);
	//从坐标为1开始,删除3个字符
	cout << s1 << endl;
}
int main() {
	text01();
	text02();
	return 0;
}

在这里插入图片描述

4. 截取子串(比较实用!!!)

4.1 函数原型

string substr(int pos=0, int n = npos) const; 返回由pos开始的n个字符组成的字符串

4.2 代码展示

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
//求子串
void text01() {
	string str = "hello";
	string subStr = str.substr(1, 3);
	cout << subStr << endl;
}

void text02() {
	string email = "zhangsan@awa.com";
	//从邮件地址获取用户名信息【即@前的名字】
	//思路:截取"@"前的字符串
	int pos = email.find("@");	//pos结果为@的下标
	string username = email.substr(0, pos - 1);
	cout << username << endl;
}

int main() {
	text01();
	text02();
	return 0;
}

在这里插入图片描述

5. 珍惜每一份相遇,感谢一路陪伴的你们💗

string的故事到这里就讲完了,让我们相约在vector容器、deque容器叭!

  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 28
    评论
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

卫冕711

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

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

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

打赏作者

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

抵扣说明:

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

余额充值