46 std::string_view string_to_search_in,
const std::string_view string_to_search_for,
const bool include_sub_string_in_result
48 const auto pos = string_to_search_in.find(string_to_search_for);
50 if (pos == std::string_view::npos)
53 return string_to_search_in.substr(0, include_sub_string_in_result ? pos + string_to_search_for.size() : pos);
66 const size_t nth, std::string_view string_to_search_in,
const std::string_view string_to_search_for,
67 const bool include_sub_string_in_result
69 size_t pos = std::string_view::npos;
71 for (
size_t i = 0; i < nth; ++i) {
72 pos = string_to_search_in.find(string_to_search_for, pos + 1);
74 if (pos == std::string_view::npos) {
79 if (pos == std::string_view::npos) {
83 return string_to_search_in.substr(0, include_sub_string_in_result ? pos + string_to_search_for.size() : pos);
95 const std::string_view string_to_search_in,
const std::string_view string_to_search_for,
const bool include_sub_string_in_result
97 const auto pos = string_to_search_in.rfind(string_to_search_for);
99 if (pos == std::string_view::npos)
102 return string_to_search_in.substr(0, include_sub_string_in_result ? pos + string_to_search_for.size() : pos);
114 std::string_view string_to_search_in,
const std::string_view string_to_search_for,
const bool include_sub_string_in_result
116 const auto pos = string_to_search_in.find(string_to_search_for);
118 if (pos == std::string_view::npos)
121 return string_to_search_in.substr(include_sub_string_in_result ? pos : pos + string_to_search_for.size());
134 const size_t nth, std::string_view string_to_search_in,
const std::string_view string_to_search_for,
135 const bool include_sub_string_in_result
137 size_t pos = std::string_view::npos;
139 for (
size_t i = 0; i < nth; ++i) {
140 pos = string_to_search_in.find(string_to_search_for, pos + 1);
142 if (pos == std::string_view::npos) {
147 if (pos == std::string_view::npos) {
151 return string_to_search_in.substr(include_sub_string_in_result ? pos : pos + string_to_search_for.size(), string_to_search_in.size());
163 const std::string_view string_to_search_in,
const std::string_view string_to_search_for,
const bool include_sub_string_in_result
165 const auto pos = string_to_search_in.rfind(string_to_search_for);
167 if (pos == std::string_view::npos) {
171 return string_to_search_in.substr(include_sub_string_in_result ? pos : pos + string_to_search_for.size());
181inline std::string_view
182string_remove_prefix(
const std::string_view&
string,
const std::string_view prefix_to_remove,
bool* found =
nullptr) {
183 const auto pos =
string.find(prefix_to_remove);
196 return string.substr(pos + prefix_to_remove.size());
205inline std::string_view
206string_remove_suffix(
const std::string_view&
string,
const std::string_view suffix_to_remove,
bool* found =
nullptr) {
207 const auto pos =
string.rfind(suffix_to_remove);
209 if (pos !=
string.size() - suffix_to_remove.size()) {
220 return string.substr(0, pos);
228inline std::string_view
string_trim(
const std::string_view input) {
235 while (start < input.size() && std::isspace(input[start])) {
240 if (start == input.size()) {
241 return std::string_view {};
245 size_t end = input.size() - 1;
246 while (end > start && std::isspace(input[end])) {
250 return input.substr(start, end - start + 1);
262template<
typename Type>
263std::enable_if_t<std::is_integral_v<Type>, std::optional<Type>>
264string_to_int(std::string_view
string,
const bool strict =
false,
const int base = 10) {
266 auto [p, ec] = std::from_chars(
string.data(),
string.data() +
string.size(), result, base);
267 if (ec == std::errc() && (!strict || p >=
string.data() +
string.size()))
279 return std::stof(str);
292 return std::stod(str);
305 return text.rfind(starts_with, 0) == 0;
314inline bool string_ends_with(
const std::string_view text,
const std::string_view ends_with) {
315 if (ends_with.length() > text.length()) {
318 return text.compare(text.length() - ends_with.length(), ends_with.length(), ends_with) == 0;
328 return std::any_of(
string.begin(),
string.end(), [c](
const char& item) {
339inline bool string_contains(
const std::string_view
string,
const std::string_view sub_string) {
340 return string.find(sub_string) != std::string_view::npos;
349inline std::vector<std::string>
string_split(
const std::string&
string,
const char* delimiter) {
350 std::vector<std::string> results;
355 while ((next =
string.find_first_of(delimiter, prev)) != std::string::npos) {
356 if (next - prev != 0) {
357 results.push_back(
string.substr(prev, next - prev));
362 if (prev <
string.size()) {
363 results.push_back(
string.substr(prev));
375inline std::vector<std::string>
string_split(
const std::string&
string,
const char delimiter) {
376 const char delimiter_string[2] = {delimiter,
'\0'};
387inline std::string
string_replace(
const std::string_view original,
const std::string_view to_replace,
const std::string_view replacement) {
388 auto modified = std::string(original);
391 while ((pos = modified.find(to_replace, pos)) != std::string::npos) {
392 modified.replace(pos, to_replace.length(), replacement);
393 pos += replacement.length();
408 if (input.size() < 2) {
412 const char first = input.front();
413 const char last = input.back();
416 if ((first ==
'"' && last ==
'"') || (first ==
'\'' && last ==
'\'')) {
417 return input.substr(1, input.size() - 2);
431 if (lhs.size() != rhs.size()) {
435 for (
size_t i = 0; i < lhs.size(); ++i) {
436 if (std::tolower(lhs[i]) != std::tolower(rhs[i])) {
450inline std::string
string_to_upper(
const std::string& str, std::size_t count = std::numeric_limits<size_t>::max()) {
451 if (str.empty() || count == 0) {
454 std::string result = str;
455 count = std::min(count, result.size());
456 for (std::size_t i = 0; i < count; ++i) {
457 result[i] =
static_cast<char>(std::toupper(
static_cast<unsigned char>(result[i])));
468inline std::string
string_to_lower(
const std::string& str, std::size_t count = std::numeric_limits<size_t>::max()) {
469 if (str.empty() || count == 0) {
472 std::string result = str;
473 count = std::min(count, result.size());
474 for (std::size_t i = 0; i < count; ++i) {
475 result[i] =
static_cast<char>(std::tolower(
static_cast<unsigned char>(result[i])));
std::string string_replace(const std::string_view original, const std::string_view to_replace, const std::string_view replacement)
Replaces all sequences of to_replace with replacement.
std::string string_to_lower(const std::string &str, std::size_t count=std::numeric_limits< size_t >::max())
Converts the first count characters of a string to lower case.
std::string_view string_from_first_occurrence_of(std::string_view string_to_search_in, const std::string_view string_to_search_for, const bool include_sub_string_in_result)
Returns a string truncated starting from to first last occurrence of a needle.
std::string_view string_trim(const std::string_view input)
Trims given string by removing whitespace at the start and the end or the string.
std::enable_if_t< std::is_integral_v< Type >, std::optional< Type > > string_to_int(std::string_view string, const bool strict=false, const int base=10)
String to number - a small convenience function around std::from_chars.
bool string_ends_with(const std::string_view text, const std::string_view ends_with)
Tests whether given text ends with a certain string.
std::string_view string_up_to_first_occurrence_of(std::string_view string_to_search_in, const std::string_view string_to_search_for, const bool include_sub_string_in_result)
Returns a string truncated up to the first occurrence of a needle.
std::string_view string_remove_prefix(const std::string_view &string, const std::string_view prefix_to_remove, bool *found=nullptr)
Returns a copy of string with given prefix removed, if prefix is found at the beginning of the string...
bool string_starts_with(const std::string_view text, const std::string_view starts_with)
Tests whether given text starts with a certain string.
std::string string_to_upper(const std::string &str, std::size_t count=std::numeric_limits< size_t >::max())
Converts the first count characters of a string to upper case.
std::string_view string_from_last_occurrence_of(const std::string_view string_to_search_in, const std::string_view string_to_search_for, const bool include_sub_string_in_result)
Returns a string truncated starting from to last occurrence of a needle.
std::string_view string_up_to_last_occurrence_of(const std::string_view string_to_search_in, const std::string_view string_to_search_for, const bool include_sub_string_in_result)
Returns a string truncated up to the last occurrence of a needle.
bool string_compare_case_insensitive(const std::string_view lhs, const std::string_view rhs)
Compares 2 strings case-insensitively.
std::optional< float > string_to_float(const std::string &str)
String to float - converts a string to a float, or returns an empty optional if the conversion failed...
std::string_view string_up_to_the_nth_occurrence_of(const size_t nth, std::string_view string_to_search_in, const std::string_view string_to_search_for, const bool include_sub_string_in_result)
Returns a string truncated up to the nth occurrence of a needle.
std::string_view string_remove_suffix(const std::string_view &string, const std::string_view suffix_to_remove, bool *found=nullptr)
Returns a copy of string with given suffix removed, if suffix is found at the end of the string.
std::string_view string_from_nth_occurrence_of(const size_t nth, std::string_view string_to_search_in, const std::string_view string_to_search_for, const bool include_sub_string_in_result)
Returns a string truncated from the nth occurrence of a needle.
std::string_view string_unquoted(std::string_view input)
If given string is quoted, it will remove the quotes.
bool string_contains(const std::string_view string, char c)
Returns whether given string contains a certain character.
std::vector< std::string > string_split(const std::string &string, const char *delimiter)
Splits a string into a vector of strings based on a delimiter.
std::optional< double > string_to_double(const std::string &str)
String to double - converts a string to a double, or returns an empty optional if the conversion fail...