回答
使用std::string::find
如下:
if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}
注: 「找到了!」將被打印,如果s2是s1的子字符串,則s1和s2的類型都是std :: string。
您可以嘗試使用find
功能:
string str ("There are two needles in this haystack.");
string str2 ("needle");
if (str.find(str2) != string::npos) {
//.. found.
}
其實,你可以嘗試使用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;
}
「我認爲std :: string沒有提供足夠的方法來執行所有常見的字符串操作」。但是對於這個任務來說,有一個「發現」方法。無需引入庫依賴關係。 – stefan 2014-06-23 06:35:00
@stefan,你是對的,有一個find方法,但是如何拆分,替換和許多其他staff.You可以比較std :: string到字符串api在Java.PS:我也認爲包含更加優雅比查找一個字符串是否包含另一個字符串。 – 2014-06-23 08:01:10
你可以試試這個
string s1 = "Hello";
string s2 = "el";
if(strstr(s1.c_str(),s2.c_str()))
{
cout << " S1 Contains S2";
}
如果你不想使用標準庫函數,下面是一個解決方案。
#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;
}
這是一個簡單的函數
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;
}
你好,歡迎來到SO。你能否請你的答案,並添加一個評論它是如何工作的,以及它與其他答案有什麼不同?謝謝! – 2017-03-27 16:45:57
#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;
}
請儘量避免將代碼轉儲爲答案,並嘗試解釋它的作用和原因。對於那些沒有相關編碼經驗的人來說,你的代碼可能並不明顯。請修改您的答案,以包含[澄清,上下文,並嘗試在答案中提及任何限制,假設或簡化。](https://stackoverflow.com/help/how-to-answer) – 2017-08-21 00:01:30
- 1. Applescript:檢查一個字符串是否包含空字符串?
- 2. 如何檢查字符串是否包含C#中的字符?
- 3. 檢查字符串是否包含字(不是子字符串!)
- 4. 檢查字符串是否包含字符集中的字符
- 5. c# - 檢查字符串是否包含字符和數字
- 6. 檢查一個字符串是否只包含特殊字符
- 7. 檢查一個字符串是否包含給定字符
- 8. 檢查一個字符串是否包含任何字符
- 9. 檢查一個字符串是否包含特定字符
- 10. 檢查一個字符串是否只包含某些字符
- 11. 檢查字符串中是否包含一組字符?
- 12. 有效檢查一個字符串是否包含另一個字符串
- 13. 如何檢查一個字符串是否包含一個子字符串 - mysql
- 14. 檢查一個字符串是否包含另一個字符串
- 15. 檢查一個字符串是否包含另一個字符串
- 16. C - 檢查字符串是否是另一個字符串的子字符串
- 17. Ruby檢查一個字符串是否包含多個不同的字符串?
- 18. 檢查字符串包含字符串
- 19. 檢查字符串是否包含除
- 20. 檢查是否字符串包含「HTTP://」
- 21. 檢查Enum是否包含字符串?
- 22. 檢查NSMutableArray是否包含字符串
- 23. 檢查行是否包含字符串
- 24. 檢查是否WCHAR包含字符串
- 25. 檢查一對字符串是否包含相同的字符?
- 26. 檢查一個字符串是否包含數字和字母
- 27. React-Native:檢查字符串是否包含字符串
- 28. 檢查字符串是否不包含其他字符串
- 29. 檢查字符串是否包含子字符串
- 30. 檢查2個字符串是否包含相同的字符?
你的意思的char *字符串或從STL字符串? – anthares 2010-02-26 08:23:39
它不是char *字符串。我不得不#include來使用它。 –
neuromancer
2010-02-26 09:37:15
某些解決方案使用s2作爲我想查找的字符串。如果我使用類似「這是一個字符串」而不是s2的方式,它會繼續工作嗎? – neuromancer 2010-02-26 10:38:27