string_初探

博客内容主要是补充了一些以前未知的string类知识,聚焦于信息技术领域中string类相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

补充了一些以前不知道的string类知识

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	//substr(pos,len),返回从pos号位开始,长度为len的元素 
	string str="Thank you for your smile";
	cout<<str.substr(0,5)<<endl;
	cout<<str.substr(14,4)<<endl;
	cout<<str.substr(19,5)<<endl;
	//string::npos是一个常数,本身=-1,usigned值,所以可以认为是4294967295
	//str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置
	//str.find(str2,7),从str的pos号位开始匹配str2,返回值同上 
	string str2="you";
	string str3="me";
	if(str.find(str2)!=string::npos)
		cout<<str.find(str2)<<endl;
	if(str.find(str2,7)!=string::npos)
		cout<<str.find(str2,7)<<endl;
	if(str.find(str3)!=string::npos)
		cout<<str.find(str3)<<endl;
	else
		cout<<"I know there is no position for me."<<endl;
	//str.replace(pos,len,str2)把str从pos号位开始,长度为len的子串替换为str2
	//str.replace(it1,it2,str3)把str的迭代器[it1,it)范围的子串替换为str2 
	str="Maybe you will turn around";
	str2="will not";
	str3="surely";
	cout<<str.replace(10,4,str2)<<endl;
	cout<<str.replace(str.begin(),str.begin()+5,str3)<<endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值