如何在c++中查找字符串中出现的字符串
How to find occurrences of a string in string in C++?
如何在c++中找到字符串的出现?
场景如下。
string base_string = "BF;1;2;3;FF;10;20;30;BF;11;;22;33;FF;100;200;300;BF;110;;220;330;FF;1000;2000;3000";
string to_find_occurances_of = "BF";
int occurrences = 0;
string::size_type start = 0;
while ((start = base_string.find(to_find_occurrences_of, start)) != string::npos) {
++occurrences;
start += to_find_occurrences_of.length(); // see the note
}
string::find
接受要在调用对象中查找的字符串和(在此重载中)开始查找的字符位置,并返回字符串出现的位置,如果没有找到字符串,则返回string::npos
。
变量start
从0(第一个字符)开始,在循环的条件下,您使用start
告诉find
从哪里开始查找,然后将find
的返回值赋给start
。增加发生次数;现在start
保持了字符串的位置,您可以跳过to_find_occurrences_of.length()
1字符并重新开始查找。
1 drhirsch指出,如果
to_find_occurrences_of
包含重复的字符序列,执行start += to_find_occurrences_of.length()
可能会跳过一些出现的字符。例如,如果base_string
是"ffff"
, to_find_occurrences_of
是"ff"
,那么如果将to_find_occurrences_of.length()
添加到start
,则只会计数2次。如果您想避免这种情况,请将1而不是to_find_occurrences_of.length()
添加到start
中,在该示例中,将计数出现3次而不是2次。下面是使用用户定义的查找函数在字符串中查找字符串的代码
int Find(char OrgStr[], char szFind[]);
void main(){
int iCount = Find("babbabaab ab", "ab");
//cout<<"No of 'abe' : " << iCount <<endl;
}
int Find(char orgStr[], char findStr[]){
int i,j,k,l,szLen,orgLen;
char temp[] = " ";
orgLen = strlen(orgStr);
szLen = strlen(findStr);
k= 0;
i = 0;
l = 0;
while( l < orgLen )
{
i = (orgLen - ( orgLen - l));
for( j = 0; j < szLen; j++)
{
temp[j] = orgStr[i];
i++;
}
temp[j] = ' ';
if(strcmp(temp,findStr) == 0)
{
k++;
}
strcpy(temp,"");
l++;
}
cout<<"No of 'ab' : " << k <<endl;
return k;
//strcpy(temp,"");
}
在这种情况下可以使用Boost c++库。下面是一个字符串匹配的例子:
#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
#include <vector>
using StringRange = boost::iterator_range<std::string::const_iterator>;
int main()
{
std::string base_string = "BF;1;2;3;FF;10;20;30;BF;11;;22;33;FF;100;200;300;BF;110;;220;330;FF;1000;2000;3000";
std::string to_find_occurances_of = "BF";
std::vector<StringRange> matches;
boost::find_all(matches, base_string, to_find_occurances_of);
for (auto match : matches) {
std::cout << "Found [" << to_find_occurances_of << "] at index " << match.begin() - base_string.begin() << ".n";
}
}
这段代码打印:
Found [BF] at index 0.
Found [BF] at index 21.
Found [BF] at index 49.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("BF;1;2;3;FF;10;20;30;BF;11;;22;33;FF;100;200;300;BF;110;;220;330;FF;1000;2000;3000");
string str2 ("BF");
size_t found;
// different member versions of find in the same order as above:
found=str.find(str2);
//print
return 0;
}
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 按类型与字符串查找对象
- 使用C RTTI(内置)通过字符串查找功能指针
- 关于获取行和字符串查找函数的问题
- 字符串查找方法找不到第一字母
- C++ 字符串.查找()
- 遇到字符串::查找的问题
- 将一个数组作为子字符串查找到另一个数组中
- 字符串查找第一个非的 C++ 问题
- 如何将通配符与字符串::查找一起使用
- 字符串::查找问题 (C++)
- 性能标准::strstr vs. 标准::字符串::查找
- C++字符串::查找崩溃应用程序
- 使用字符串查找单词的正确方法是什么
- 如何在<string>没有 std::string 中介的情况下制作一个支持通过 C 字符串查找的集合?
- 使用C++根据行中的第一个字符串查找行(仅一个)
- Borland字符串::查找bug
- std::map如何通过字符串查找元素
- 字符串查找函数返回奇数
- 如何使用子字符串查找数字行的正确部分