到目前为止,我有这个

if(tempString.find(mString) != string::npos) //if found word
{
    cout<<endl<<tempString<<endl; //this prints the entire line
}


例如,如果tempString为“我不知道,因为他”而mString为“我知道”,它将显示“我不知道”

我知道tempString.find(mString)返回子字符串的位置。我如何使用它从子字符串开始打印

最佳答案

使用std::string::substr

size_t pos = tempString.find(mString);
if (pos != string::npos)
{
  std::string to_print = tempString.substr(pos);
  cout << to_print;
}

关于c++ - 如何找到子字符串的位置并从那里打印,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22084183/

10-11 12:16