我有一个std::string类型的变量。我想检查它是否包含一个特定的std::字符串。我该怎么做呢?
是否有一个函数,如果找到字符串返回true,如果没有找到则返回false ?
我有一个std::string类型的变量。我想检查它是否包含一个特定的std::字符串。我该怎么做呢?
是否有一个函数,如果找到字符串返回true,如果没有找到则返回false ?
当前回答
使用std::string::find如下所示:
if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}
注意:如果s2是s1的子字符串,则打印"found!", s1和s2都是std::string类型。
其他回答
从这个网站上的这么多答案中,我没有找到一个明确的答案,所以在5-10分钟内我自己找到了答案。 但这可以在两种情况下实现:
要么你知道你在字符串中搜索的子字符串的位置 要么你不知道它的位置,然后逐字符搜索它……
所以,让我们假设我们在字符串“abcde”中搜索子字符串“cd”,我们使用c++中最简单的substr内置函数
1:
#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:
#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;
}
使用std::string::find如下所示:
if (s1.find(s2) != std::string::npos) {
std::cout << "found!" << '\n';
}
注意:如果s2是s1的子字符串,则打印"found!", s1和s2都是std::string类型。
你可以尝试使用find函数:
string str ("There are two needles in this haystack.");
string str2 ("needle");
if (str.find(str2) != string::npos) {
//.. found.
}
我们可以用这个方法代替。 这是我项目中的一个例子。 参考代码。 一些额外的费用也包括在内。
看看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";
}
}
}
如果不想使用标准库函数,下面是一种解决方案。
#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.\n";
else
std::cout << "Second string is not a substring of the first string.\n";
return 0;
}