我有一个std::string类型的变量。我想检查它是否包含一个特定的std::字符串。我该怎么做呢?
是否有一个函数,如果找到字符串返回true,如果没有找到则返回false ?
我有一个std::string类型的变量。我想检查它是否包含一个特定的std::字符串。我该怎么做呢?
是否有一个函数,如果找到字符串返回true,如果没有找到则返回false ?
当前回答
是什么
string response = "hello world";
string findMe = "world";
if(response.find(findMe) != string::npos)
{
//found
}
其他回答
#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;
}
实际上,你可以尝试使用boost库,我认为std::string没有提供足够的方法来做所有常见的字符串操作。在boost中,你可以只使用boost::algorithm::包含:
#include <string>
#include <boost/algorithm/string.hpp>
int main() {
std::string s("gengjiawen");
std::string t("geng");
bool b = boost::algorithm::contains(s, t);
std::cout << b << std::endl;
return 0;
}
我们可以用这个方法代替。 这是我项目中的一个例子。 参考代码。 一些额外的费用也包括在内。
看看if语句!
/*
Every C++ program should have an entry point. Usually, this is the main function.
Every C++ Statement ends with a ';' (semi-colon)
But, pre-processor statements do not have ';'s at end.
Also, every console program can be ended using "cin.get();" statement, so that the console won't exit instantly.
*/
#include <string>
#include <bits/stdc++.h> //Can Use instead of iostream. Also should be included to use the transform function.
using namespace std;
int main(){ //The main function. This runs first in every program.
string input;
while(input!="exit"){
cin>>input;
transform(input.begin(),input.end(),input.begin(),::tolower); //Converts to lowercase.
if(input.find("name") != std::string::npos){ //Gets a boolean value regarding the availability of the said text.
cout<<"My Name is AI \n";
}
if(input.find("age") != std::string::npos){
cout<<"My Age is 2 minutes \n";
}
}
}
注意:我知道这个问题需要一个函数,这意味着用户试图找到一些更简单的东西。但我还是把它贴出来,以防有人觉得有用。
使用后缀自动机的方法。它接受一个字符串(干草堆),然后你可以输入成千上万的查询(针),并且响应将非常快,即使干草堆和/或针是非常长的字符串。
阅读此处使用的数据结构:https://en.wikipedia.org/wiki/Suffix_automaton
#include <bits/stdc++.h>
using namespace std;
struct State {
int len, link;
map<char, int> next;
};
struct SuffixAutomaton {
vector<State> st;
int sz = 1, last = 0;
SuffixAutomaton(string& s) {
st.assign(s.size() * 2, State());
st[0].len = 0;
st[0].link = -1;
for (char c : s) extend(c);
}
void extend(char c) {
int cur = sz++, p = last;
st[cur].len = st[last].len + 1;
while (p != -1 && !st[p].next.count(c)) st[p].next[c] = cur, p = st[p].link;
if (p == -1)
st[cur].link = 0;
else {
int q = st[p].next[c];
if (st[p].len + 1 == st[q].len)
st[cur].link = q;
else {
int clone = sz++;
st[clone].len = st[p].len + 1;
st[clone].next = st[q].next;
st[clone].link = st[q].link;
while (p != -1 && st[p].next[c] == q) st[p].next[c] = clone, p = st[p].link;
st[q].link = st[cur].link = clone;
}
}
last = cur;
}
};
bool is_substring(SuffixAutomaton& sa, string& query) {
int curr = 0;
for (char c : query)
if (sa.st[curr].next.count(c))
curr = sa.st[curr].next[c];
else
return false;
return true;
}
// How to use:
// Execute the code
// Type the first string so the program reads it. This will be the string
// to search substrings on.
// After that, type a substring. When pressing enter you'll get the message showing the
// result. Continue typing substrings.
int main() {
string S;
cin >> S;
SuffixAutomaton sa(S);
string query;
while (cin >> query) {
cout << "is substring? -> " << is_substring(sa, query) << endl;
}
}
使用std::regex_search也不错。让搜索更通用的垫脚石。下面是一个带有注释的例子。
//THE STRING IN WHICH THE SUBSTRING TO BE FOUND.
std::string testString = "Find Something In This Test String";
//THE SUBSTRING TO BE FOUND.
auto pattern{ "In This Test" };
//std::regex_constants::icase - TO IGNORE CASE.
auto rx = std::regex{ pattern,std::regex_constants::icase };
//SEARCH THE STRING.
bool isStrExists = std::regex_search(testString, rx);
需要包含#include <regex>
出于某种原因,假设输入字符串被观察到类似于“在这个示例字符串中查找一些东西”,并且有兴趣搜索“在这个测试中”或“在这个示例中”,那么可以通过简单地调整如下所示的模式来增强搜索。
//THE SUBSTRING TO BE FOUND.
auto pattern{ "In This (Test|Example)" };