How to do it...

  1. To convert a string to lowercase or uppercase, apply the tolower() or toupper() functions on the characters of a string using the general purpose algorithm std::transform():
        template<typename CharT> 
inline tstring<CharT> to_upper(tstring<CharT> text)
{
std::transform(std::begin(text), std::end(text),
std::begin(text), toupper);
return text;
}

template<typename CharT>
inline tstring<CharT> to_lower(tstring<CharT> text)
{
std::transform(std::begin(text), std::end(text),
std::begin(text), tolower);
return text;
}
  1. To reverse a string, use the general purpose algorithm std::reverse():
        template<typename CharT> 
inline tstring<CharT> reverse(tstring<CharT> text)
{
std::reverse(std::begin(text), std::end(text));
return text;
}
  1. To trim a string, at the beginning, end, or both, use std::basic_string's methods find_first_not_of() and find_last_not_of():
        template<typename CharT> 
inline tstring<CharT> trim(tstring<CharT> const & text)
{
auto first{ text.find_first_not_of(' ') };
auto last{ text.find_last_not_of(' ') };
return text.substr(first, (last - first + 1));
}

template<typename CharT>
inline tstring<CharT> trimleft(tstring<CharT> const & text)
{
auto first{ text.find_first_not_of(' ') };
return text.substr(first, text.size() - first);
}

template<typename CharT>
inline tstring<CharT> trimright(tstring<CharT> const & text)
{
auto last{ text.find_last_not_of(' ') };
return text.substr(0, last + 1);
}
  1. To trim characters in a given set from a string, use overloads of std::basic_string's methods find_first_not_of() and find_last_not_of(), that take a string parameter that defines the set of characters to look for:
        template<typename CharT> 
inline tstring<CharT> trim(tstring<CharT> const & text,
tstring<CharT> const & chars)
{
auto first{ text.find_first_not_of(chars) };
auto last{ text.find_last_not_of(chars) };
return text.substr(first, (last - first + 1));
}

template<typename CharT>
inline tstring<CharT> trimleft(tstring<CharT> const & text,
tstring<CharT> const & chars)
{
auto first{ text.find_first_not_of(chars) };
return text.substr(first, text.size() - first);
}

template<typename CharT>
inline tstring<CharT> trimright(tstring<CharT> const &text,
tstring<CharT> const &chars)
{
auto last{ text.find_last_not_of(chars) };
return text.substr(0, last + 1);
}
  1. To remove characters from a string, use std::remove_if() and std::basic_string::erase():
        template<typename CharT> 
inline tstring<CharT> remove(tstring<CharT> text,
CharT const ch)
{
auto start = std::remove_if(
std::begin(text), std::end(text),
[=](CharT const c) {return c == ch; });
text.erase(start, std::end(text));
return text;
}
  1. To split a string based on a specified delimiter, use std::getline() to read from an std::basic_stringstream initialized with the content of the string. The tokens extracted from the stream are pushed into a vector of strings:
        template<typename CharT> 
inline std::vector<tstring<CharT>> split
(tstring<CharT> text, CharT const delimiter)
{
auto sstr = tstringstream<CharT>{ text };
auto tokens = std::vector<tstring<CharT>>{};
auto token = tstring<CharT>{};
while (std::getline(sstr, token, delimiter))
{
if (!token.empty()) tokens.push_back(token);
}
return tokens;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset