String management and utilities
Header: #include <mrpt/system/string_utils.h>
.
Library: [mrpt-system]
// global functions template <typename T> T mrpt::system::str2num(std::string const& value); char* mrpt::system::strtok(char* str, const char* strDelimit, char** context); template <class OUT_CONTAINER> void mrpt::system::tokenize( const std::string& inString, const std::string& inDelimiters, OUT_CONTAINER& outTokens, bool skipBlankTokens = true ); template void mrpt::system::tokenize< std::deque< std::string > >( const std::string& inString, const std::string& inDelimiters, std::deque<std::string>& outTokens, bool skipBlankTokens ); template void mrpt::system::tokenize< std::vector< std::string > >( const std::string& inString, const std::string& inDelimiters, std::vector<std::string>& outTokens, bool skipBlankTokens ); std::string mrpt::system::trim(const std::string& str); std::string mrpt::system::upperCase(const std::string& str); std::string mrpt::system::lowerCase(const std::string& str); void mrpt::system::decodeUTF8(const std::string& strUTF8, std::vector<uint16_t>& out_uniStr); void mrpt::system::encodeUTF8(const std::vector<uint16_t>& input, std::string& output); void mrpt::system::encodeBase64(const std::vector<uint8_t>& inputData, std::string& outString); bool mrpt::system::decodeBase64(const std::string& inString, std::vector<uint8_t>& outData); std::string mrpt::system::unitsFormat(const double val, int nDecimalDigits = 2, bool middle_space = true); std::string mrpt::system::rightPad( const std::string& str, size_t total_len, bool truncate_if_larger = false ); bool mrpt::system::strCmp(const std::string& s1, const std::string& s2); bool mrpt::system::strCmpI(const std::string& s1, const std::string& s2); bool mrpt::system::strStarts(const std::string& str, const std::string& subStr); bool mrpt::system::strStartsI(const std::string& str, const std::string& subStr); template <typename T> std::string mrpt::system::sprintf_container(const char* fmt, const T& V); void mrpt::system::stringListAsString( const std::vector<std::string>& lst, std::string& out, const std::string& newline = "\r\n" ); void mrpt::system::stringListAsString( const std::deque<std::string>& lst, std::string& out, const std::string& newline = "\r\n" ); size_t mrpt::system::nthOccurrence(const std::string& str, const std::string& strToFind, size_t nth); std::string mrpt::system::firstNLines(const std::string& str, size_t n);
Global Functions
template <typename T> T mrpt::system::str2num(std::string const& value)
Original code snippet found in http://stackoverflow.com/a/30357710.
Convert string instance to number
char* mrpt::system::strtok(char* str, const char* strDelimit, char** context)
An OS-independent method for tokenizing a string.
The extra parameter “context” must be a pointer to a “char*” variable, which needs no initialization and is used to save information between calls to strtok.
See also:
template <class OUT_CONTAINER> void mrpt::system::tokenize( const std::string& inString, const std::string& inDelimiters, OUT_CONTAINER& outTokens, bool skipBlankTokens = true )
Tokenizes a string according to a set of delimiting characters.
Example:
std::vector<std::string> tokens; tokenize(" - Pepe-Er Muo"," -",tokens);
Will generate 3 tokens:
“Pepe”
“Er”
“Muo”
Parameters:
skipBlankTokens |
If |
OUT_CONTAINER |
Can be a std::vector or std::deque of std::string’s. |
std::string mrpt::system::trim(const std::string& str)
Removes leading and trailing spaces.
std::string mrpt::system::upperCase(const std::string& str)
Returns a upper-case version of a string.
See also:
std::string mrpt::system::lowerCase(const std::string& str)
Returns an lower-case version of a string.
See also:
void mrpt::system::decodeUTF8( const std::string& strUTF8, std::vector<uint16_t>& out_uniStr )
Decodes a UTF-8 string into an UNICODE string.
See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
void mrpt::system::encodeUTF8( const std::vector<uint16_t>& input, std::string& output )
Encodes a 2-bytes UNICODE string into a UTF-8 string.
See http://en.wikipedia.org/wiki/UTF-8 and http://www.codeguru.com/cpp/misc/misc/multi-lingualsupport/article.php/c10451/.
void mrpt::system::encodeBase64( const std::vector<uint8_t>& inputData, std::string& outString )
Encode a sequence of bytes as a string in base-64.
See also:
bool mrpt::system::decodeBase64( const std::string& inString, std::vector<uint8_t>& outData )
Decode a base-64 string into the original sequence of bytes.
Returns:
false on invalid base-64 string passed as input, true on success.
See also:
std::string mrpt::system::unitsFormat( const double val, int nDecimalDigits = 2, bool middle_space = true )
This function implements formatting with the appropriate SI metric unit prefix: 1e-12->’p’, 1e-9->’n’, 1e-6->’u’, 1e-3->’m’, 1->’’, 1e3->’K’, 1e6->’M’, 1e9->’G’, 1e12->’T’.
See also:
std::string mrpt::system::rightPad( const std::string& str, size_t total_len, bool truncate_if_larger = false )
Enlarge the string with spaces up to the given length.
bool mrpt::system::strCmp(const std::string& s1, const std::string& s2)
Return true if the two strings are equal (case sensitive)
See also:
StrCmpI
bool mrpt::system::strCmpI(const std::string& s1, const std::string& s2)
Return true if the two strings are equal (case insensitive)
See also:
StrCmp
bool mrpt::system::strStarts(const std::string& str, const std::string& subStr)
Return true if “str” starts with “subStr” (case sensitive)
See also:
bool mrpt::system::strStartsI(const std::string& str, const std::string& subStr)
Return true if “str” starts with “subStr” (case insensitive)
See also:
template <typename T> std::string mrpt::system::sprintf_container( const char* fmt, const T& V )
Generates a string for a container in the format [A,B,C,…], and the fmt string for each vector element.
void mrpt::system::stringListAsString( const std::vector<std::string>& lst, std::string& out, const std::string& newline = "\r\n" )
Convert a string list to one single string with new-lines.
void mrpt::system::stringListAsString( const std::deque<std::string>& lst, std::string& out, const std::string& newline = "\r\n" )
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
size_t mrpt::system::nthOccurrence( const std::string& str, const std::string& strToFind, size_t nth )
Finds the position of the n-th occurence of the given substring, or std::string::npos if it does not happen.
New in MRPT 2.3.2
std::string mrpt::system::firstNLines(const std::string& str, size_t n)
Returns the first n
lines (splitted by ‘
‘ chars) of the given text.
New in MRPT 2.3.2