replace_all这样常用的操作在C++却没直接提供,只好自己写个备忘。// replace all occurance of t in s to w
void replace_all(std::string & s, std::string const & t, std::string const & w)
{
unsigned int pos = s.find(t), t_size = t.size(), r_size = w.size();
while(pos != std::string::npos){ // found
s.replace(pos, t_size, w);
pos = s.find(t, pos + r_size );
}
}
C++,替换字符串的全部目标子串
最新推荐文章于 2024-06-07 20:28:43 发布
本文详细介绍了如何在C++中实现字符串替换函数replace_all,该函数用于替换字符串中的特定子串。通过使用标准库提供的find和replace方法,实现对目标字符串中所有指定子串的替换操作。
2418

被折叠的 条评论
为什么被折叠?



