[Boost.StringAlgo] How to insert substr into str

Dear All,
what is the elegant/right way to insert some substr into the given str?
For example:
// -------------------------------------------------------
...
std::string str("xyz_123_abc_xyyz_a1b");
std::string mark("abc");
std::string substr("$$$");
iterator_range

Valentine Rozental wrote:
what is the elegant/right way to insert some substr into the given str? For example:
// ------------------------------------------------------- ... std::string str("xyz_123_abc_xyyz_a1b"); std::string mark("abc"); std::string substr("$$$");
iterator_range
std::string::type> it1 = boost::find_first(str, mark); ... // ------------------------------------------------------- I want to insert substr ("$$$") after mark ("abc") if the mark exists in the str. That is, the result in the example above should be: "xyz_123_abc$$$_xyyz_a1b"
This is a non-boost answer, but I'd have written something like: std::string::size_type found = str.find(mark); if (found != std::string::npos) { str.insert(found + mark.length(), substr); }

How about:
#include

Hi, Valentine Rozental wrote:
Dear All,
what is the elegant/right way to insert some substr into the given str? For example:
// ------------------------------------------------------- ... std::string str("xyz_123_abc_xyyz_a1b"); std::string mark("abc"); std::string substr("$$$");
iterator_range
std::string::type> it1 = boost::find_first(str, mark); ... // ------------------------------------------------------- I want to insert substr ("$$$") after mark ("abc") if the mark exists in the str. That is, the result in the example above should be: "xyz_123_abc$$$_xyyz_a1b"
Thank you in advance!
If you are using std::string, why could simply type if(it1) { str.insert(it1.end()-1, mark); } Regards, Pavol.
participants (4)
-
Bruno Lalande
-
Nat Goodspeed
-
Pavol Droba
-
Valentine Rozental