#include <ext/string>
Collects helpers under ext::string for common string transformations and compatibility behavior. It also provides optional movable string aliases and a u8string fallback for compilers without native char8_t support.
ext::string::printable,lprintable, andrprintableremove non-printable characters from the whole string, left edge, or right edge.ext::string::trim,ltrim, andrtrimremove whitespace from the whole string, left edge, or right edge.ext::string::search(text, needle, case_sensitive)returns whether a needle appears in text. Matching is ASCII case-insensitive by default.ext::string::starts_with(text, prefix, case_sensitive)checks a prefix.ext::string::equal(lhs, rhs, case_sensitive)compares full strings.ext::string::replace_all(text, old, replacement, case_sensitive)replaces every occurrence of a string.ext::string::split(text, delimiter)returns non-empty string tokens.ext::string::split<T>(text, delimiter, converter)maps non-empty tokens through a converter callback.ext::string::back(text)returns the last character or zero for an empty string.ext::string::convert(src, dst, locale)converts betweenstd::stringandstd::wstring.ext::string::convert<DstChar>(src, locale)returns a converted string or throws when exceptions are enabled.operator!(std::string)andoperator!(std::wstring)are shorthand conversions between narrow and wide strings.ext::from_u8,from_u8string, andto_u8stringbridge UTF-8 string types whenCXX_USE_STD_U8STRINGor nativestd::u8stringsupport is available.ext::string::basic_string<T>derives fromstd::basic_string<T>for legacy movable-string compatibility when that compatibility path is enabled.
- Most helpers are templated for
charandwchar_tstring types. - In-place overloads mutate the supplied string and return a reference to it.
const std::basic_string<T>, pointer, and rvalue overloads return transformed copies where supported by the compiler. - The
ext::u8stringfallback usesunsigned charstorage when nativestd::u8stringis unavailable. search,starts_with,equal, andreplace_allare ASCII case-insensitive by default. Passtruefor case-sensitive matching.splitskips empty tokens. For example, splitting"a,,b"by","returns{"a", "b"}.convertusesstd::codecvtwith the provided locale. Conversion behavior therefore depends on the platform locale and standard-library support.from_u8string(..., ignore_errors)currently substitutes invalid UTF-8 sequences with a replacement character in the wide conversion path.
- GCC 8.3.0+
- Clang 10.0+
- Visual Studio 2008 SP1+
-
Text cleanup and matching
#include <ext/string> std::string text = "\tHello\n"; ext::trim(text); // "Hello" ext::search("Hello world", "WORLD"); // true ext::search("Hello world", "WORLD", true); // false ext::starts_with("Hello", "he"); // true ext::equal("Value", "value"); // true
-
replace and split
#include <ext/string> std::string text = "one two one"; ext::replace_all(text, "one", "1"); // text == "1 two 1" std::vector<std::string> list = ext::split("a,b,,c,d", ","); // list == {"a", "b", "c", "d"} std::vector<int> numbers = ext::split<int>("1|2|3", "|", [](const std::string &s) { return std::stoi(s); });
-
narrow/wide conversion
#include <ext/string> std::wstring wide = ext::string::convert<wchar_t>(std::string("text")); std::string narrow = ext::string::convert<char>(wide); std::wstring wide2 = !std::string("text"); std::string narrow2 = !wide2;
-
u8string
#define CXX_USE_STD_U8STRING #include <ext/string> #if defined(CXX_STD_U8STRING_NOT_SUPPORTED) #if defined(__cpp_user_defined_literals) && \ (CXX_VER >= __cpp_user_defined_literals) std::u8string str = ext::from_u8(u8"한글+english"); // str contains UTF-8 bytes for "한글+english". #else std::u8string str = ext::from_u8(ext::to_u8string(L"한글+english")); // str contains UTF-8 bytes for "한글+english". #endif #else std::u8string str = u8"한글+english"; // str contains UTF-8 bytes for "한글+english". #endif