开发者

find last location in a string

开发者 https://www.devze.com 2023-02-08 07:10 出处:网络
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.

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()] = '.';
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号