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


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

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

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

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

#include <iostream>
#include <string>
#include <algorithm>
/*
 *Find all positions of the a SubString in given String
 */
void findAllOccurances(std::vector<size_t> & 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<size_t> 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,"<<std::endl;
    for(size_t pos : vec)
        std::cout<<pos<<std::endl;
    return 0;
}

输出打印如下:

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

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

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

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

#include <iostream>
#include <string>
#include <algorithm>
// 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<size_t> & 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<size_t> 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,"<<std::endl;
    for(size_t pos : vec)
        std::cout<<pos<<std::endl;
    return 0;
}

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

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

一种通用的方法

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

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

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
// 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<size_t> & 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<size_t> vec;
    std::cout<<"All Index Position of Case InSensitive 'is' in given string are,"<<std::endl;
    // Find All case insensitive occurrences of the 'is' in the vector 'vec'
    findAllOccurances(vec, data , "iS", &findCaseInsensitive);
    for(size_t pos : vec)
        std::cout<<pos<<std::endl;
    vec.clear();
    std::cout<<"All Index Position of Case Sensitive 'is' in given string are,"<<std::endl;
    // Get All Case Sensitive occurrences of the 'is' in the vector 'vec'
    findAllOccurances(vec, data , "iS", [](std::string data, std::string toSearch, size_t pos){
        return data.find(toSearch, pos);
    });
    for(size_t pos : vec)
        std::cout<<pos<<std::endl;
    return 0;
}

打印结果如下:

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

 

 

 

0



error: 内容受到保护 !!