11 namespace StringUtils {
17 template<
typename T,
typename D>
18 [[nodiscard]]
static std::string
join(
const std::span<T>& con,
const D& delim) {
19 std::ostringstream oss{};
20 for (std::size_t i = 0; i < con.size(); ++i) {
22 if (i != con.size() - 1) {
28 template<
typename T,
typename D>
29 [[nodiscard]]
static std::string
join(
const std::vector<T>& con,
const D& delim) {
30 return join(std::span{con}, delim);
37 [[nodiscard]]
static std::vector<std::string>
split(
const std::string& str,
char delim) {
38 std::vector<std::string> res;
39 std::stringstream ss{str};
41 while (std::getline(ss, chunk, delim))
49 [[nodiscard]]
static std::string
toLowerCopy(std::string str) {
50 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
57 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
63 [[nodiscard]]
static std::string
toUpperCopy(std::string str) {
64 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
71 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
76 static void ltrim(std::string& str) {
77 str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](
unsigned char ch) {
78 return !std::isspace(ch);
84 static void rtrim(std::string& str) {
85 str.erase(std::find_if(str.rbegin(), str.rend(), [](
unsigned char ch) {
86 return !std::isspace(ch);
94 static void trim(std::string& str) {
102 [[nodiscard]]
static std::string
ltrimCopy(std::string s) {
110 [[nodiscard]]
static std::string
rtrimCopy(std::string s) {
118 [[nodiscard]]
static std::string
trimCopy(std::string s) {
128 [[nodiscard]]
static bool startsWith(
const std::string& str,
const std::string& prefix,
bool ignoreCase =
false) {
129 if (str.size() < prefix.size())
return false;
131 return std::equal(prefix.begin(), prefix.end(), str.begin(),
132 [](
char a,
char b) { return std::tolower(a) == std::tolower(b); });
133 return str.compare(0, prefix.size(), prefix) == 0;
141 [[nodiscard]]
static bool endsWith(
const std::string& str,
const std::string& suffix,
bool ignoreCase =
false) {
142 if (str.size() < suffix.size())
return false;
144 return std::equal(suffix.begin(), suffix.end(), str.begin() + str.size() - suffix.size(),
145 [](
char a,
char b) { return std::tolower(a) == std::tolower(b); });
146 return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
153 static void replace(std::string& str,
const std::string& search,
const std::string&
replace) {
154 if (search.empty())
return;
156 while ((pos = str.find(search, pos)) != std::string::npos) {
157 str.replace(pos, search.length(),
replace);
168 [[nodiscard]]
static std::string
replaceCopy(std::string str,
const std::string& search,
const std::string&
replace) {
169 if (search.empty())
return str;
171 while ((pos = str.find(search, pos)) != std::string::npos) {
172 str.replace(pos, search.length(),
replace);
183 [[nodiscard]]
static T
to(
const std::string& str) {
185 std::istringstream oss{str};
191 [[nodiscard]]
static std::string
random(std::size_t length) {
192 static std::random_device seed{};
193 static std::default_random_engine engine{seed()};
194 static std::uniform_int_distribution<short> choice(0, 2);
195 static std::uniform_int_distribution<int> lowercaseAlpha(
'a',
'z');
196 static std::uniform_int_distribution<int> uppercaseAlpha(
'A',
'Z');
197 static std::uniform_int_distribution<int> digits(
'0',
'9');
199 std::string str(length,
'\000');
201 switch (choice(engine)) {
203 c = lowercaseAlpha(engine);
206 c = uppercaseAlpha(engine);
static bool startsWith(const std::string &str, const std::string &prefix, bool ignoreCase=false)
Returns true if str starts with prefix.
static void toLower(std::string &str)
Lowercase original string str.
static std::string toUpperCopy(std::string str)
Uppercase a string.
static std::string join(const std::span< T > &con, const D &delim)
Form a string from a container with a delimiter.
static void rtrim(std::string &str)
Right trim a string from end (in place)
static std::string replaceCopy(std::string str, const std::string &search, const std::string &replace)
static std::string trimCopy(std::string s)
Left and Right trim a string from start and end (copy)
static void trim(std::string &str)
Left and right trim a string (from beginning and end) (in place)
static std::vector< std::string > split(const std::string &str, char delim)
Split a string by delimiter.
static void replace(std::string &str, const std::string &search, const std::string &replace)
static std::string random(std::size_t length)
Returns a random string of length characters.
static std::string ltrimCopy(std::string s)
Left trim a string from start (copy)
static void toUpper(std::string &str)
Uppercase original string str.
static void ltrim(std::string &str)
Left trim a string from start (in place)
static std::string rtrimCopy(std::string s)
Right trim a string from end (copy)
static bool endsWith(const std::string &str, const std::string &suffix, bool ignoreCase=false)
Returns true if str ends with suffix.
static std::string toLowerCopy(std::string str)
Lowercase a string.
static T to(const std::string &str)