CppCommon  1.0.4.1
C++ Common Library
string_utils.cpp

String utilities example

#include <iostream>
int main(int argc, char** argv)
{
std::cout << CppCommon::StringUtils::ToLower('A') << std::endl;
std::cout << CppCommon::StringUtils::ToUpper('a') << std::endl;
std::cout << CppCommon::StringUtils::ToLower("aBcDeFg") << std::endl;
std::cout << CppCommon::StringUtils::ToUpper("aBcDeFg") << std::endl;
std::cout << '"' << CppCommon::StringUtils::ToLTrim(" foobar ") << '"' << std::endl;
std::cout << '"' << CppCommon::StringUtils::ToRTrim(" foobar ") << '"' << std::endl;
std::cout << '"' << CppCommon::StringUtils::ToTrim(" foobar ") << '"' << std::endl;
std::cout << '"' << CppCommon::StringUtils::RemoveBlank(" \t foo \t bar \t ") << '"' << std::endl;
std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", '!') << std::endl;
std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", 'z') << std::endl;
std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", "foo") << std::endl;
std::cout << CppCommon::StringUtils::Contains("a foo a bar a baz", "foobar") << std::endl;
std::cout << CppCommon::StringUtils::CountAll("a foo a bar a baz", "a ") << std::endl;
std::string str;
str = "a foo a bar a baz";
std::cout << str << std::endl;
str = "a foo a bar a baz";
std::cout << str << std::endl;
str = "a foo a bar a baz";
std::cout << str << std::endl;
std::cout << CppCommon::StringUtils::Join(CppCommon::StringUtils::Split("a foo a bar a baz", ' '), '+') << std::endl;
std::cout << CppCommon::StringUtils::Join(CppCommon::StringUtils::Split("a foo a bar a baz", "a "), "the ") << std::endl;
std::cout << CppCommon::StringUtils::ToString(true) << std::endl;
std::cout << CppCommon::StringUtils::ToString(false) << std::endl;
std::cout << CppCommon::StringUtils::ToString('0') << std::endl;
std::cout << CppCommon::StringUtils::ToString((uint8_t)49) << std::endl;
std::cout << CppCommon::StringUtils::ToString(100) << std::endl;
std::cout << CppCommon::StringUtils::ToString(123.456) << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("true") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("yes") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("on") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("1") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("false") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("no") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("off") << std::endl;
std::cout << CppCommon::StringUtils::FromString<bool>("0") << std::endl;
std::cout << CppCommon::StringUtils::FromString<char>("0") << std::endl;
std::cout << CppCommon::StringUtils::FromString<uint8_t>("49") << std::endl;
std::cout << CppCommon::StringUtils::FromString<int>("100") << std::endl;
std::cout << CppCommon::StringUtils::FromString<double>("123.456") << std::endl;
return 0;
}
static size_t CountAll(std::string_view str, std::string_view substr)
Count all occurrences of substring.
static std::vector< std::string > Split(std::string_view str, char delimiter, bool skip_empty=false)
Split the string into tokens by the given delimiter character.
static std::string ToRTrim(std::string_view str)
Trims space characters from the end of the given constant string.
static std::string ToString(const T &value)
Converts arbitrary datatypes into string using std::ostringstream.
static bool ReplaceAll(std::string &str, std::string_view substr, std::string_view with)
Replace all occurrences of substring with another substring.
static std::string ToLTrim(std::string_view str)
Trims space characters from the start of the given constant string.
static std::string Join(const std::vector< std::string > &tokens, bool skip_empty=false, bool skip_blank=false)
Join tokens into the string.
static char ToLower(char ch)
Convert the given character to lower case.
static bool ReplaceFirst(std::string &str, std::string_view substr, std::string_view with)
Replace the first occurrence of substring with another substring.
static std::string ToTrim(std::string_view str)
Trims space characters from the both sides of the given constant string.
static char ToUpper(char ch)
Convert the given character to UPPER case.
static bool ReplaceLast(std::string &str, std::string_view substr, std::string_view with)
Replace the last occurrence of substring with another substring.
static bool Contains(std::string_view str, const char ch)
Is the given string contains the given character?
static std::string RemoveBlank(std::string_view str)
Remove blank characters from the given string.
String utilities definition.