2010-02-26 136 views
289

我有一个字符串类型的变量。我想检查它是否包含某个字符串。我会怎么做?检查一个字符串是否包含C++中的字符串

是否有一个函数在找到字符串时返回true,如果不是,则返回false?

+4

你的意思的char *字符串或从STL字符串? – anthares 2010-02-26 08:23:39

+0

它不是char *字符串。我不得不#include 来使用它。 – neuromancer 2010-02-26 09:37:15

+0

某些解决方案使用s2作为我想查找的字符串。如果我使用类似“这是一个字符串”而不是s2的方式,它会继续工作吗? – neuromancer 2010-02-26 10:38:27

回答

438

使用std::string::find如下:

if (s1.find(s2) != std::string::npos) { 
    std::cout << "found!" << '\n'; 
} 

注: “找到了!”将被打印,如果s2是s1的子字符串,则s1和s2的类型都是std :: string。

76

您可以尝试使用find功能:

string str ("There are two needles in this haystack."); 
string str2 ("needle"); 

if (str.find(str2) != string::npos) { 
//.. found. 
} 
15

其实,你可以尝试使用Boost库,我想的std :: string不能提供足够的方法来完成所有的常见字符串operation.In提升,你可以只使用boost::algorithm::contains

#include "string" 

#include "boost/algorithm/string.hpp" 

using namespace std; 
using namespace boost; 
int main(){ 
    string s("gengjiawen"); 
    string t("geng"); 
    bool b = contains(s, t); 
    cout << b << endl; 
    return 0; 
} 
+15

“我认为std :: string没有提供足够的方法来执行所有常见的字符串操作”。但是对于这个任务来说,有一个“发现”方法。无需引入库依赖关系。 – stefan 2014-06-23 06:35:00

+4

@stefan,你是对的,有一个find方法,但是如何拆分,替换和许多其他staff.You可以比较std :: string到字符串api在Java.PS:我也认为包含更加优雅比查找一个字符串是否包含另一个字符串。 – 2014-06-23 08:01:10

4

你可以试试这个

string s1 = "Hello"; 
string s2 = "el"; 
if(strstr(s1.c_str(),s2.c_str())) 
{ 
    cout << " S1 Contains S2"; 
} 
4

如果你不想使用标准库函数,下面是一个解决方案。

#include <iostream> 
#include <string> 

bool CheckSubstring(std::string firstString, std::string secondString){ 
    if(secondString.size() > firstString.size()) 
     return false; 

    for (int i = 0; i < firstString.size(); i++){ 
     int j = 0; 
     if(firstString[i] == secondString[j]){ 
      while (firstString[i] == secondString[j] && j < secondString.size()){ 
       j++; 
       i++; 
      } 

      if (j == secondString.size()) 
        return true; 
     } 
    } 
    return false; 
} 

int main(){ 
    std::string firstString, secondString; 

    std::cout << "Enter first string:"; 
    std::getline(std::cin, firstString); 

    std::cout << "Enter second string:"; 
    std::getline(std::cin, secondString); 

    if(CheckSubstring(firstString, secondString)) 
     std::cout << "Second string is a substring of the frist string.\n"; 
    else 
     std::cout << "Second string is not a substring of the first string.\n"; 

    return 0; 
} 
1

这是一个简单的函数

bool find(string line, string sWord) 
{ 
    bool flag = false; 
    int index = 0, i, helper = 0; 
    for (i = 0; i < line.size(); i++) 
    { 
     if (sWord.at(index) == line.at(i)) 
     { 
      if (flag == false) 
      { 
       flag = true; 
       helper = i; 
      } 
      index++; 
     } 
     else 
     { 
      flag = false; 
      index = 0; 
     } 
     if (index == sWord.size()) 
     { 
      break; 
     } 
    } 
    if ((i+1-helper) == index) 
    { 
     return true; 
    } 
    return false; 
} 
+1

你好,欢迎来到SO。你能否请你的答案,并添加一个评论它是如何工作的,以及它与其他答案有什么不同?谢谢! – 2017-03-27 16:45:57

-1
#include <algorithm>  // std::search 
#include <string> 
using std::search; using std::count; using std::string; 

int main() { 
    string mystring = "The needle in the haystack"; 
    string str = "needle"; 
    string::const_iterator it; 
    it = search(mystring.begin(), mystring.end(), 
       str.begin(), str.end()) != mystring.end(); 

    // if string is found... returns iterator to str's first element in mystring 
    // if string is not found... returns iterator to mystring.end() 

if (it != mystring.end()) 
    // string is found 
else 
    // not found 

return 0; 
} 
+3

请尽量避免将代码转储为答案,并尝试解释它的作用和原因。对于那些没有相关编码经验的人来说,你的代码可能并不明显。请修改您的答案,以包含[澄清,上下文,并尝试在答案中提及任何限制,假设或简化。](https://stackoverflow.com/help/how-to-answer) – 2017-08-21 00:01:30

相关问题