字符串查找函数 find()函数

本文详细介绍了C++中字符串find()函数的各种用法,包括查找子串首次出现的位置、从特定位置开始查找、查找所有出现的位置以及从字符串末尾开始查找。通过多个实例演示了如何使用这些函数进行字符串搜索。


       find()函数可以帮助你在两个字符串之间,查找很多他们的关系。。。

#include<iostream>
#include<string>
using namespace std;

int main()
{

	string s,s2;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>s>>s2;
		string flag;
        int position = s.find(s2);
       if (position != s.npos) 
	
        cout << "position is : " << position << endl;
 
       else
 
       cout << "Not found the flag" + flag;
	}
	return 0;
}
/查找s2在s中的第一个位置
#include<iostream>
#include<string>
using namespace std;

int main()
{

	string s,s2;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>s>>s2;
	   string	flag = s2;
      int position = s.find_first_of(flag);
      cout << "s.find_first_of(flag) is : " << position << endl;
	}
	return 0;
}
#include<iostream>
#include<string>
using namespace std;

int main()
{

	string s,s2;
	int n,m;
	cin>>n;
	while(n--)
	{
		cin>>s>>s2;
		cin>>m;
		int position=s.find(s2,m);//在s中从第n的位置开始查找第一个s2的位置
        cout<<"s.find(s2,m) is : "<<position<<endl;
	}
	return 0;
}

#include<iostream>
#include<string>
using namespace std;

int main()
{

	string s,s2;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>s>>s2;
	string	flag=s2;
     int   position=0;
      int i=1;
     while( (position=s.find_first_of(flag,position) )!=string::npos )//在s中查找所有s2的位置并依次输出
	 {
         //position=s.find_first_of(flag,position);
         cout<<"position  "<<i<<" : "<<position<<endl;
         position++;
         i++;
	 }
	}
	 return 0;
}
//在s中从最后一位开始查找s2的第一个位置
#include<iostream>
#include<string>
using namespace std;

int main()
{

	string s,s2;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>s>>s2;
		string	flag=s2;
        int   position=0;
        position=s.rfind (flag);
        cout<<"s.rfind (flag) :"<<position<<endl;
	}
	return 0;
}*/

在以上的例子中,我们可以熟练掌握一些题型。

http://acm.nyist.net/JudgeOnline/problem.php?pid=5

代码:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1,s2;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>s1>>s2;
		unsigned int m=s2.find(s1,0);
		int num=0;
		while(m!=string::npos)
		{
			num++;
			m=s2.find(s1,m+1);
		}
		cout<<num<<endl;
	}
	return 0;
}


转载于:https://www.cnblogs.com/NYNU-ACM/p/4236784.html

Python中有多个用于字符串查找函数,下面为你详细介绍: ### find()函数 `find()`函数用于在字符串查找特定的子字符串,其查找过程类似在一本厚厚的电话簿中从第一页开始逐页翻看,直到找到目标名字。该函数返回子字符串第一次出现的索引位置,如果未找到则返回 -1。示例如下: ```python # 定义一个字符串 string = "Python is a great programming language" # 查找字符串 "is" 的位置 index = string.find("is") print(index) # 输出:7 ``` ### rfind()函数 `rfind()`函数是从右向左查找字符串,即倒序查找。它返回子字符串最后一次出现的索引位置,如果未找到则返回 -1。例如: ```python string = "Python is a great programming language, is it?" # 从右向左查找 "is" 的位置 index = string.rfind("is") print(index) # 输出:35 ``` ### 自定义从右向左查找函数 也可通过编写自定义函数实现从右向左查找字符串的功能,以下是一个示例: ```python def right_to_left_find(string, string_find): finally_num = string[::-1].find(string_find) if finally_num != -1: result = string[len(string) - finally_num - len(string_find):] else: result = "" return result print(right_to_left_find('今日更新文件.来自王先生.xlsx', '.')) # 返回结果:xlsx ``` ### strspn()函数 `strspn()`函数用于扫描字符串是否包含指定的字符。不过在 Python 3 中,原示例代码的写法有误,正确使用方式如下: ```python sStr1 = '12345678' sStr2 = '456' # 计算 sStr1 中从开头开始的字符都在 sStr2 中的字符长度 import string length = string.strspn(sStr1, sStr2) print(length) ``` ### index()函数 虽然引用中未提及,但`index()`函数也是常用的字符串查找函数。它和`find()`函数类似,用于查找字符串第一次出现的索引位置,但不同的是,如果未找到子字符串,`index()`函数会抛出`ValueError`异常。示例如下: ```python string = "Python is a great programming language" try: index = string.index("is") print(index) # 输出:7 except ValueError: print("未找到子字符串") ``` ### rindex()函数 `rindex()`函数与`rfind()`函数类似,从右向左查找字符串最后一次出现的索引位置,若未找到则抛出`ValueError`异常。示例如下: ```python string = "Python is a great programming language, is it?" try: index = string.rindex("is") print(index) # 输出:35 except ValueError: print("未找到子字符串") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值