【问题标题】:How to find occurrences of a string in string in C++? [duplicate]如何在 C++ 中查找字符串中出现的字符串? [复制]
【发布时间】:2011-12-23 09:15:32
【问题描述】:

如何在 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";

【问题讨论】:

  • @BjörnPollex 删除标签#c-strings

标签: c++


【解决方案1】:
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 获取要在调用对象中查找的字符串和(在此重载中)开始查找的字符位置,并返回字符串出现的位置,如果字符串是没找到。

变量start从0(第一个字符)开始,在循环的条件下,你使用start告诉find从哪里开始查找,然后将find的返回值赋给@ 987654328@。增加发生次数;现在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 次。

【讨论】:

  • 如果 to_find_occurrances_of 包含重复的字符或序列,您将跳过一些“出现”。
  • @SethCarnegie 谢谢,它的工作。
  • @DreamCodeer 如果您已经知道这一点,请原谅我,但如果此答案回答了您的问题,请通过单击答案旁边的复选标记将其标记为已回答。
  • @SethCarnegie .. 再次感谢.. 标记为已回答。
  • 太棒了!无论是小写字母还是大写字母,我必须怎么做才能使其正常工作?
【解决方案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] = '\0';
        if(strcmp(temp,findStr) == 0)
        {
            k++;
        }
        strcpy(temp,"");
        l++;
    }
    cout<<"No of 'ab' : " << k <<endl;
    return k;
    //strcpy(temp,"");
}    

【讨论】:

  • 完美运行!谢谢1
【解决方案3】:

在这种情况下,您可以使用Boost C++ library。下面是一个字符串匹配的例子:

#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.

【讨论】:

    【解决方案4】:
    #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;
    }
    

    【讨论】:

    • 谢谢。用过第一个。
    • 这里的 size_t 是什么?
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 2011-04-15
    • 2019-07-14
    • 2013-04-21
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    相关资源
    最近更新 更多