📜  C++ find()查找子字符串

📅  最后修改于: 2020-08-29 09:29:57             🧑  作者: Mango

在之前的文章中,我们介绍了替换子字符串的方法。

今天,我们继续了解查找子字符串,主要介绍利用find()函数,在区别大小写和不区分大小写的情况下,查找子字符串。

查找子字符串,区分大小写

使用std :: string :: find搜索第一个匹配项,然后从该位置开始使用相同的逻辑继续搜索,直到到达结尾为止。

#include 
#include 
#include 
/*
 *Find all positions of the a SubString in given String
 */
void findAllOccurances(std::vector & vec, std::string data, std::string toSearch)
{
    // Get the first occurrence
    size_t pos = data.find(toSearch);
    // Repeat till end is reached
    while( pos != std::string::npos)
    {
        // Add position to the vector
        vec.push_back(pos);
        // Get the next occurrence from the current position
        pos =data.find(toSearch, pos + toSearch.size());
    }
}
int main()
{
    std::string data = "Hi this is a Sample string, 'iS' is here 3 times";
    std::vector vec;
    // Get All occurrences of the 'is' in the vector 'vec'
    findAllOccurances(vec, data , "is");
    std::cout<<"All Index Position of 'is' in given string are,"<

输出打印如下:

由于区分大小写,故找到3个’is’

All Index Position of 'is' in given string are,
5
8
33

查找子字符串,不区分大小写

我们将使用与上述相同的逻辑,即继续搜索子字符串的出现,直到到达结尾为止。但在查找时,将字符串进行了转换!

#include 
#include 
#include 
// Case Insensitive version of std::string::find
size_t findCaseInsensitive(std::string data, std::string toSearch, size_t pos = 0)
{
    // Convert String to lower case
    std::transform(data.begin(), data.end(), data.begin(), ::tolower);
    // Convert String to lower case
    std::transform(toSearch.begin(), toSearch.end(), toSearch.begin(), ::tolower);
    return data.find(toSearch, pos);
}
/*
 *Find all positions of the a Case Insensitive Sub String in given String
 */
void findAllOccurancesCaseInsensitive(std::vector & vec, std::string data, std::string toSearch)
{
    // Find First Position
    size_t pos = findCaseInsensitive(data,toSearch);
    // Iterate till end
    while( pos != std::string::npos)
    {
        // Push position in vector
        vec.push_back(pos);
        // Search next position
        pos = findCaseInsensitive(data,toSearch, pos + toSearch.size());
    }
}
int main()
{
    std::string data = "Hi this is a Sample string, 'iS' is here 4 times";
    std::vector vec;
    // Get All case insensitive occurrences of the 'is' in the vector 'vec'
    findAllOccurancesCaseInsensitive(vec, data , "IS");
    std::cout<<"All Index Position of 'is' in given string are,"<

打印输出如下:也正是由于在查找之前,字符串进行了转换,从而巧妙的绕过了大小写的区分。

All Index Position of 'is' in given string are,
5
8
29
33

一种通用的方法

在上面的示例中,需要维护2中方法,在这里演示一种更通用的方法来实现区分/不区分大小写的字符串查找。

先定义了一个回调函数,然后根据不同的需求,调用不同的方法。

#include 
#include 
#include 
#include 
#include 
// Case Insensitive version of std::string::find
size_t findCaseInsensitive(std::string data, std::string toSearch, size_t pos = 0)
{
    // Convert String to lower case
    std::transform(data.begin(), data.end(), data.begin(), ::tolower);
    // Convert String to lower case
    std::transform(toSearch.begin(), toSearch.end(), toSearch.begin(), ::tolower);
    return data.find(toSearch, pos);
}
typedef size_t (* FINDFUNCTOR)(std::string data, std::string toSearch, size_t pos);
void findAllOccurances(std::vector & vec, std::string data, std::string toSearch, FINDFUNCTOR finder)
{
    // Find first occurrence
    size_t pos = finder(data, toSearch, 0);
    // Iterate till last
    while( pos != std::string::npos)
    {
        vec.push_back(pos);
        // Find next occurrence
        pos =finder(data, toSearch, pos + toSearch.size());
    }
}
int main()
{
    std::string data = "Hi this is a Sample string, iS is 3 times here";
    std::vector vec;
    std::cout<<"All Index Position of Case InSensitive 'is' in given string are,"<

打印结果如下:

All Index Position of Case InSensitive 'is' in given string are,
5
8
28
31
All Index Position of Case Sensitive 'iS' in given string are,
28