目录
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==0cout<<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;
}
运行结果
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
#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负责从这个位置向后获得字符串