CC15.【C++ Cont】string类字符串的find和substr函数

目录

1.find函数

作用

返回值

四种格式

1.size_t find (const string& str, size_t pos = 0) const;

示例代码1

运行结果

示例代码2

运行结果

2.size_t find (const char* s, size_t pos = 0) const;

示例代码3

运行结果

示例代码4

 运行结果

3.size_t find (const char* s, size_t pos, size_t n) const;

示例代码5

运行结果

4.size_t find (char c, size_t pos = 0) const;

示例代码6

运行结果

5.字符串找不到的情况

示例代码7

运行结果

​编辑

解析npos的值

2.substr函数

作用

用法

1.截取整个字符串

示例代码8

运行结果

2.从某一下标开始截取到最后

示例代码9

运行结果

3.从某一下标开始截取到某一下标

示例代码10

运行结果

3.find和substr函数的组合使用


1.find函数

cplusplus网的介绍 点我跳转

作用

用于查找字符串中指定子串/字符,并返回子串/字符在字符串中第一次出现的位置(类似于C语言的strstr函数55.【C语言】字符函数和字符串函数(strstr函数),但find函数功能更强大!)

返回值

1.若找到,则返回子串/字符在字符串中第一次出现的起始下标位置。
2.若未找到,则返回一个整数值npos

因此通常判断find()函数的返回值是否等于npos就能知道是否查找到子串或者字符。

四种格式

下面均以字符串Hello World Hello World做例子

1.size_t find (const string& str, size_t pos = 0) const;

string类的字符串进行查找,查找开始的位置为pos,如果pos不写,默认从头开始查找

示例代码1
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
	string s2="World";
	size_t find_pos=s1.find(s2);//pos参数不写,默认从头开始查找,即pos==0
	cout<<find_pos; 
	return 0;
}
运行结果

下标为6是World第一次出现的位置

示例代码2
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
	string s2="World";
	size_t find_pos=s1.find(s2,11);//pos参数为11 
	cout<<find_pos; 
	return 0;
}
运行结果

下标为18是World第二次出现的位置

2.size_t find (const char* s, size_t pos = 0) const;

C语言风格的字符串进行查找,查找开始的位置为pos,如果pos不写,默认从头开始查找

示例代码3
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
	size_t find_pos=s1.find("World");//pos参数不写,默认从头开始查找(即pos==0)
	cout<<find_pos; 
	return 0;
}
运行结果

下标为6是World第一次出现的位置

示例代码4
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
	size_t find_pos=s1.find("World",11);//pos参数为11 
	cout<<find_pos; 
	return 0;
}
 运行结果

下标为6是World第一次出现的位置

3.size_t find (const char* s, size_t pos, size_t n) const;

C语言风格的字符串的前n个字符组成的字符串进行查找,查找开始的位置为pos,如果pos不写,默认从头开始查找

示例代码5
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
    //查找Word的前三个字符组成的字符串Wor第一次出现的位置 
	size_t find_pos=s1.find("Word",0,3);
	cout<<find_pos; 
	return 0;
}
运行结果

4.size_t find (char c, size_t pos = 0) const;

字符c进行查找,查找开始的位置为pos,如果pos不写,默认从头开始查找

示例代码6
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
	size_t find_pos=s1.find("W");//查找字符W第一次出现的位置,默认从头开始查找 
	cout<<find_pos; 
	return 0;
}
运行结果

注:由于find函数的返回值为size_t,因此需要用类型为size_t的变量接收

5.字符串找不到的情况

示例代码7

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1="Hello World Hello World";
	size_t find_pos=s1.find("abc");
	if (find_pos=string::npos)
		cout<<"找不到";
	else cout<<find_pos; 
	return 0;
}
运行结果

解析npos的值

cplusplus网的介绍 点我跳转

注意到npos的值为-1,但size_t是无符号的int类型,因此将-1以无符号的形式存储,可以打印出来看看

#include <iostream>
#include <string>
using namespace std;
int main()
{
	size_t npos_number=string::npos;
	cout<<npos_number; 
	return 0;
}

运行结果

换算成十六进制为0xFFFFFFFF,即4GB,在x86架构的32位系统中虚拟地址空间的理论上限

2.substr函数

cplusplus网的介绍 点我跳转

作用

用于截取字符串中指定位置指定长度的子串

用法

从substr()函数的声明string substr (size_t pos = 0, size_t len = npos) const;可以看出

一定注意第二个参数为截取的字符串的长度!!!!!!

1.截取整个字符串

即substr()不传任何参数

示例代码8
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s0="Hello World Hello World";
	string s1=s0.substr(); 
	cout<<s1<<endl;
	return 0;
}
运行结果

2.从某一下标开始截取到最后

示例代码9

从下标3位置一直截取到字符串的末尾

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s0="Hello World Hello World";
	string s2=s0.substr(3);
	cout<<s2<<endl;
	return 0;
}
运行结果

3.从某一下标开始截取到某一下标

示例代码10

从下标3截取长度为5的字符串,不是下标为5!!!!!!

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s0="Hello World Hello World";
	string s3=s0.substr(3,5);
	cout<<s3<<endl; 
	return 0;
}
运行结果


3.find和substr函数的组合使用

注意:在竞赛中,find函数和substr函数经常配合使用,find 负责找到位置,subst负责从这个位置向后获得字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhangcoder

赠人玫瑰手有余香,感谢支持~

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

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

打赏作者

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

抵扣说明:

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

余额充值