Check if a string contains a string in C++
我有一个
如果找到字符串,是否有返回true的函数;如果没有,是否有返回false的函数?
使用
1 2 3 4 | if (s1.find(s2) != std::string::npos) { std::cout <<"found!" << ' '; } |
注:"找到!"如果
您可以尝试使用
1 2 3 4 5 6 | string str ("There are two needles in this haystack."); string str2 ("needle"); if (str.find(str2) != string::npos) { //.. found. } |
实际上,您可以尝试使用boost库,我认为std::string没有提供足够的方法来执行所有常见的字符串操作。在boost中,您只需使用
1 2 3 4 5 6 7 8 9 10 11 12 13 | #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; } |
你可以试试这个
1 2 3 4 5 6 | string s1 ="Hello"; string s2 ="el"; if(strstr(s1.c_str(),s2.c_str())) { cout <<" S1 Contains S2"; } |
如果您不想使用标准库函数,下面是一个解决方案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #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 the first characters match if(firstString[i] == secondString[j]){ int k = i; while (firstString[i] == secondString[j] && j < secondString.size()){ j++; i++; } if (j == secondString.size()) return true; else // Re-initialize i to its original value i = k; } } 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. "; else std::cout <<"Second string is not a substring of the first string. "; return 0; } |
从这个网站上的很多答案中,我没有找到一个明确的答案,所以在5-10分钟内,我自己就找到了答案。但这可以在两种情况下完成:
因此,假设我们在字符串"ABCDE"中搜索子字符串"CD",我们使用C++中最简单的子字符串内置函数。
1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> #include <string> using namespace std; int i; int main() { string a ="abcde"; string b = a.substr(2,2); // 2 will be c. Why? because we start counting from 0 in a string, not from 1. cout <<"substring of a is:" << b << endl; return 0; } |
2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> #include <string> using namespace std; int i; int main() { string a ="abcde"; for (i=0;i<a.length(); i++) { if (a.substr(i,2) =="cd") { cout <<"substring of a is:" << a.substr(i,2) << endl; // i will iterate from 0 to 5 and will display the substring only when the condition is fullfilled } } return 0; } |
这是一个简单的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include // 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; } |