ShlubluLib  v0.5
ShlubluLib is a lightweight, modular, general purpose, open-source C++ library for Linux and Windows.
Functions
shlublu::String Namespace Reference

Functions

template<typename T >
std::string xtos (T arg)
 Converts arithmetic values or pointers to std::string. More...
 
template<typename T >
std::string xtofs (T arg)
 Converts floating point values to std::string in fixed notation. More...
 
std::vector< std::string > const & split (std::string const &s, char delim, std::vector< std::string > &elems)
 Splits a string delimited by a given character and stores the substrings in the given target vector. More...
 
std::vector< std::string > split (std::string const &s, char delim)
 Splits a string delimited by a given character and returns the result as a vector of substrings. More...
 
std::string & ltrim (std::string &s)
 Trims the leading blank characters of a string. More...
 
std::string & rtrim (std::string &s)
 Trims the trailing blank characters of a string. More...
 
std::string & trim (std::string &s)
 Trims the leading and trailing blank characters of a string. More...
 
std::string & replace (std::string &source, std::string const &find, std::string const &replaceBy)
 Replaces all occurences of a substring in a string. More...
 
std::string & lower (std::string &s)
 Converts a string to lowercase. More...
 
std::string lower (std::string const &s)
 Returns a lowercase version of the given string. More...
 
std::string & upper (std::string &s)
 Converts a string to uppercase. More...
 
std::string upper (std::string const &s)
 Returns a uppercase version of the given string. More...
 
std::wstring toWString (std::string const &str)
 Returns an UTF-8 wstring version of the given string. More...
 
std::string fromWString (std::wstring const &wstr)
 Returns a string version of the given UTF-8 wstring. More...
 
size_t levenshteinDistance (std::string const &s, std::string const &t)
 Computes the Levenshtein distance between two strings. More...
 

Detailed Description

Helper functions for std::string.

Function Documentation

◆ xtos()

template<typename T >
std::string shlublu::String::xtos ( arg)
inline

Converts arithmetic values or pointers to std::string.

Parameters
argthe value to convert
Returns
the string representation of arg

Example

const std::string valOf42(String::xtos(42)); // "42"
const std::string valOf42AndAHalf(String::xtos(42.5)); // "42.5"
const std::string valOfThis(this); // for example: "000000013F75193C"

◆ xtofs()

template<typename T >
std::string shlublu::String::xtofs ( arg)
inline

Converts floating point values to std::string in fixed notation.

Parameters
argthe value to convert
Returns
the string representation of arg in fixed notation

Example

const std::string valOfFloat42(String::xtos(42.0f)); // "42.000000"
const std::string valOfDouble42AndAHalf(String::xtos(42.5)); // "42.500000"

◆ split() [1/2]

std::vector<std::string> const& shlublu::String::split ( std::string const &  s,
char  delim,
std::vector< std::string > &  elems 
)

Splits a string delimited by a given character and stores the substrings in the given target vector.

'elems' is cleared before receiving the resulting substrings.

Parameters
sthe string to split
delimthe delimiter
elemsthe vector to store the resulting substrings
Returns
elems

Example

std::vector<std::string> res;
String::split("my;delimited;string", ';', res); // res is { "my", "delimited", "string"}

◆ split() [2/2]

std::vector<std::string> shlublu::String::split ( std::string const &  s,
char  delim 
)

Splits a string delimited by a given character and returns the result as a vector of substrings.

Parameters
sthe string to split
delimthe delimiter
Returns
a vector of strings that stores the substrings

Example

const auto res(String::split("my;delimited;string", ';', res)); // res is { "my", "delimited", "string"}

◆ ltrim()

std::string& shlublu::String::ltrim ( std::string &  s)

Trims the leading blank characters of a string.

Parameters
sthe string to trim
Returns
s
See also
std::isspace

Example

std::string s("\t test\t ");
String::ltrim(s); // s is "test\t "

◆ rtrim()

std::string& shlublu::String::rtrim ( std::string &  s)

Trims the trailing blank characters of a string.

Parameters
sthe string to trim
Returns
s
See also
std::isspace

Example

std::string s("\t test\t ");
String::rtrim(s); // s is "\t test"

◆ trim()

std::string& shlublu::String::trim ( std::string &  s)

Trims the leading and trailing blank characters of a string.

Parameters
sthe string to trim
Returns
s
See also
std::isspace

Example

std::string s("\t test\t ");
String::trim(s); // s is "test"

◆ replace()

std::string& shlublu::String::replace ( std::string &  source,
std::string const &  find,
std::string const &  replaceBy 
)

Replaces all occurences of a substring in a string.

Parameters
sourcethe string that contains substrings to replace
findthe substring to be replaced
replaceBythe replacement substring
Returns
source
Exceptions
std::invalid_argumentif find is empty

Example

std::string s("Blue is blue");
String::replace(s, "blue", "red"); // s is "Blue is red"

◆ lower() [1/2]

std::string& shlublu::String::lower ( std::string &  s)

Converts a string to lowercase.

Parameters
sthe string to convert
Returns
source

Example

std::string s("BoTh CaSeS !");
String::lower(s); // s is "both cases !"

◆ lower() [2/2]

std::string shlublu::String::lower ( std::string const &  s)

Returns a lowercase version of the given string.

Parameters
sthe string to convert
Returns
the lowercase version of source

Example

const std::string s("BoTh CaSeS !");
const auto res(String::lower(s)); // res is "both cases !"

◆ upper() [1/2]

std::string& shlublu::String::upper ( std::string &  s)

Converts a string to uppercase.

Parameters
sthe string to convert
Returns
source

Example

std::string s("BoTh CaSeS !");
String::upper(s); // s is "BOTH CASES !"

◆ upper() [2/2]

std::string shlublu::String::upper ( std::string const &  s)

Returns a uppercase version of the given string.

Parameters
sthe string to convert
Returns
the uppercase version of source

Example

const std::string s("BoTh CaSeS !");
const auto res(String::upper(s)); // res is "BOTH CASES !"

◆ toWString()

std::wstring shlublu::String::toWString ( std::string const &  str)

Returns an UTF-8 wstring version of the given string.

Parameters
strthe string to convert
Returns
the UTF-8 wstring version of str
See also
std::codecvt_utf8<wchar_t>

Example

const auto res(String::toWString("This is a test string.")); // res is L"This is a test string."

◆ fromWString()

std::string shlublu::String::fromWString ( std::wstring const &  wstr)

Returns a string version of the given UTF-8 wstring.

Parameters
wstrthe UTF-8 wstring to convert
Returns
the string version of wstr
See also
std::codecvt_utf8<wchar_t>

Example

const auto res(String::fromWString(L"This is a test string.")); // res is "This is a test string."

◆ levenshteinDistance()

size_t shlublu::String::levenshteinDistance ( std::string const &  s,
std::string const &  t 
)

Computes the Levenshtein distance between two strings.

Parameters
sa string
tanother string
Returns
the Levenshtein distance between the two

Example

const auto res(String::levenshteinDistance("a", "abc")); // res is 2
shlublu::String::fromWString
std::string fromWString(std::wstring const &wstr)
Returns a string version of the given UTF-8 wstring.
shlublu::String::toWString
std::wstring toWString(std::string const &str)
Returns an UTF-8 wstring version of the given string.
shlublu::String::rtrim
std::string & rtrim(std::string &s)
Trims the trailing blank characters of a string.
shlublu::String::trim
std::string & trim(std::string &s)
Trims the leading and trailing blank characters of a string.
shlublu::String::replace
std::string & replace(std::string &source, std::string const &find, std::string const &replaceBy)
Replaces all occurences of a substring in a string.
shlublu::String::lower
std::string & lower(std::string &s)
Converts a string to lowercase.
shlublu::String::xtos
std::string xtos(T arg)
Converts arithmetic values or pointers to std::string.
Definition: String.h:35
shlublu::String::split
std::vector< std::string > const & split(std::string const &s, char delim, std::vector< std::string > &elems)
Splits a string delimited by a given character and stores the substrings in the given target vector.
shlublu::String::levenshteinDistance
size_t levenshteinDistance(std::string const &s, std::string const &t)
Computes the Levenshtein distance between two strings.
shlublu::String::ltrim
std::string & ltrim(std::string &s)
Trims the leading blank characters of a string.
shlublu::String::upper
std::string & upper(std::string &s)
Converts a string to uppercase.