Situations where we need to erase something in a string, replace a part of the string, or erase the first or last occurrence of some sub-string are very common. Standard library allows us to do more parts of this, but it usually involves too much code writing.
We saw the Boost.StringAlgorithm
library in action in the Changing cases and case-insensitive comparison recipe. Let's see how it can be used to simplify our lives when we need to modify some strings:
#include <string> const std::string str = "Hello, hello, dear Reader.";
This recipe shows how different string-erasing and replacing methods from the Boost.StringAlgorithm
library work:
- Erasing requires the
#include <boost/algorithm/string/erase.hpp>
header:
#include <boost/algorithm/string/erase.hpp> void erasing_examples() { namespace ba = boost::algorithm; using std::cout; cout << "\n erase_all_copy...