如何查找子字符串的位置并从那里打印它

how to find location of a substring and print it from there

本文关键字:位置 从那里 打印 字符串 何查找 查找      更新时间:2023-10-16

到目前为止,我有这个

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

例如,如果 tempString 是"For he I not know",而 mString 是 "I do",它将打印 "I not know"

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

use std::string::substr:

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