C++中对字符串的各种处理函数汇总(目标:应对ccf第三道题)

一、find()函数:查找一个字符串是否包含特定另一个字符串

find()函数带有一个参数,两个参数和三个参数的情况。

find函数带一个参数,就是直接查找;

find函数带有两个参数,就是在被查找的字符串的某个特定位置开始查找;

find函数带有三个参数,就是在被查找的字符串的某个特定位置开始查找:查找字符串的前n个字符组成的字符串!

#include <iostream>
#include <cstring>
using namespace std;
int main(){
	string s1="The first step is as good as half over.";
	string s2 = "step";
	//返回查找到的字符(串)s2在s1中的首地址
	int found = s1.find(s2);
	cout<<found<<endl;
	//---------------------------------------
	
	//返回查找到的字符(串)s2在s1中的首地址,此时,是从s1的第 n个字符开始寻找
	found = s1.find(s2,3);
	cout<<found<<endl;
	//如果不存在这个字符串,那么返回的是-1
	found = s1.find(s2,14);
	cout<<found<<endl;
	//-----------------------------------------
	
	found = s1.find("step");
	cout<<found<<endl;
	//-------------------------------------------
	
	//表示从 s1 中第9个字符开始寻找 “ism”的前两个字符
	found = s1.find("ism",9,2);
	cout<<found<<endl;
	//-------------------------------------------
	return 0;
} 

运行结果:

10
10
-1
10
15

二、find_first_of()函数:查找字符第一次出现在被查找字符串中的位置

函数find_first_of() 查找在字符串中第1个出现的字符c,而函数find_last_of()查找最后一个出现的c。匹配的位置是返回值。如果没有匹配发生,则函数返回-1.

int find_first_of(char c, int start = 0):查找字符串中第1个出现的c,由位置start开始。

如果有匹配,则返回匹配位置;否则,返回-1。默认情况下,start为0,函数搜索整个字符串。

#include <iostream>
#include <cstring>
using namespace std;
int main(){
	string s1="The first step is as good as half over.";
	string s2 = "step";
	//字符第一次出现在字符串中的位置 
	int found = s1.find_first_of("a");
	cout<<found<<endl;
	found = s1.find_first_of("as");
	cout<<found<<endl;
	cout<<"-----------------------"<<endl;
	int index=0;
	while((index=s1.find_first_of("s",index))!=-1) {
		cout<<index<<endl;
		index++;
	}
}

运行结果:

18
7
--------------------------
7
10
16
19
27

三、find_last_of()函数

int find_last_of(char c):查找字符串中最后一个出现的c。有匹配,则返回匹配位置;否则返回-1.

该搜索在字符末尾查找匹配,所以起始位置默认为字符串末尾,除非有参数指定起始位置。

#include <iostream>
#include <cstring>
using namespace std;
int main(){
	string s1="The first step is as good as half over.";
	string s2 = "s";
	//从后往前找,与first相反而已 
	int index=s1.length();
	while((index=s1.find_last_of("s",index))!=-1) {
		cout<<index<<endl;
		index--;
	}
}

运行结婚:

27
19
16
10
7

四、string substr(int start,int count)函数

带一个参数和带两个参数

#include <iostream>
#include <cstring>
using namespace std;
int main(){
	string s1="The first step is as good as half over.";
	string s2 = s1.substr(2,3);
	cout<<s2<<endl;
	s2=s1.substr(0,3);
	cout<<s2<<endl;
	//-----------------------------------------
	s2=s1.substr(4);
	cout<<s2<<endl;
}

运行结果:

e f
The
first step is as good as half over.

五、insert()函数

字符连接(+)是在字符串尾添加字符串。

insert()函数扩展了这个能力,允许在任意位置添加字符串。

void insert(int statr,const string& s): 将子串s放入字符串中,起始于位置start。插入操作增加了原始字符串的长度。

#include <iostream>
#include <cstring>
using namespace std;
int main(){
	string s1="The first step ";
	string s2 = "is as good as half over.";
	string s= s1+s2;
	cout<<s<<endl;
	s1.insert(2,s2);
	cout<<s1<<endl;
}
运行结果:
The first step is as good as half over.
This as good as half over.e first step

六、void erase(int start=0,int count=-1)函数:

从start开始,从字符串中删除count个字符。

如果现有的字符串少于count个字符,或者count为-1,则删除到字符串尾部的所有字符。默认情况下,start为0,函数从字符串是起始位置开始删除字符串。默认情况下,函数也删除到字符串尾。


    


  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值