C++ 将字符串的特定部分与另一个字符串进行比较 intmain() { char*a=“我疯了”; 字符串str(a); cout
您需要对C++ 将字符串的特定部分与另一个字符串进行比较 intmain() { char*a=“我疯了”; 字符串str(a); cout,c++,string,visual-studio,visual-c++,C++,String,Visual Studio,Visual C++,您需要对rfind()的结果进行比较 intmain() { const char*a=“我疯了”; 字符串str(a); 根据文档,if(str.rfind(“mad”)==string::npos)//string::npos表示未找到 { cout如果你能找到mad的起始索引,那么就从它创建一个子索引。 str2=str.substr(起始索引为'mad',长度为'mad') 然后,您可以使用str2与其他进行比较。您可以将返回索引与string::npos进行比较 int main()
rfind()
的结果进行比较
intmain()
{
const char*a=“我疯了”;
字符串str(a);
根据文档,if(str.rfind(“mad”)==string::npos)//string::npos表示未找到
{
cout如果你能找到mad的起始索引,那么就从它创建一个子索引。
str2=str.substr(起始索引为'mad',长度为'mad')
然后,您可以使用str2与其他进行比较。您可以将返回索引与string::npos进行比较
int main()
{
const char *a = "i am mad";
string str(a);
if (str.rfind("mad") == string::npos) // string::npos means not found, according to the docs
{
cout << "All is well" << endl;
}
else
{
cout << "Uh oh, somebody's mad!" << endl;
}
system("pause");
return 0;
}
你可能也对正则表达式感兴趣。它们会让你做到这一点,还有更多。。。
请看这里的初学者:(如果你不知道正则表达式是什么),这里是C++的STL正则表达式的实现:我对编程很陌生。你能告诉我怎么做吗?
int main()
{
const char *a = "i am mad";
string str(a);
if (str.rfind("mad") == string::npos) // string::npos means not found, according to the docs
{
cout << "All is well" << endl;
}
else
{
cout << "Uh oh, somebody's mad!" << endl;
}
system("pause");
return 0;
}
int ret = str.rfind("mad", str.length());
if (ret != string::npos)
{
// mad found in str
}
else
{
// mad not found
}