2010-02-26 134 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

相關問題