C++ does not have the concept of extension
s like other languages do, so your options are limited. Realistically, your only options are:
Create your own string
class that is composed of a std::string
and offers its own modifiers
Inherit from std::string
to provide your own functions. This really isn't a good idea from an OO-perspective, but it is technically an option.
Write a utility function to do this for you. This is the ideal/idiomatic solution.
In the latter case, the common way of doing this would be with either a free-function (in a namespace), or a static function in a StringUtilities
class. You can also modify the input string to save on unnecessary copies, if that's desirable, e.g.:
// Take 's' by mutable reference so it modifies s in place
// returns the same reference to allow chaining, e.g.
//
// to_lower(s).find(...)
auto to_lower(std::string& s) -> std::string& {
/* to lower logic */
return s;
}
// or, as a static function:
class StringUtilities {
static auto to_lower(std::string& s) -> std::string& { /* same as above */ }
};