find last location in a string
i have a string
a = "hai my name is prasanth Madhavan"
and i would like to replace the space btw prasanth and Madhavan with a period.
whe开发者_StackOverflown i use a.find("prasanth") it gives the location of p i.e the begining of the search string. is ther a way to get the location of the ending of the string using find??
Add the length of the string you are looking for.
#include <string>
std::string str("hai my name is prasanth Madhavan");
std::string prasanth("prasanth");
std::size_t prasanth_pos = str.find(prasanth);
if (prasanth_pos != std::string::npos && prasanth_pos + prasanth.size() != str.size())
str[prasanth_pos + prasanth.size()] = '.';
精彩评论