Public Types | Public Member Functions | Static Public Member Functions | List of all members
StringTools Class Reference

Public Types

typedef std::map< std::string, std::string > conv_map_t
 

Public Member Functions

template<class T1 , class T2 , class S >
bool split2 (const std::string &s, T1 &first, T2 &second, const S &separators, const std::string &trimChars)
 
template<>
const std::string & lazyConvert (const std::string &str, std::string &tmp)
 
template<>
std::string & import (const std::string &src, std::string &dst)
 

Static Public Member Functions

static bool startsWith (const std::string &s, const std::string &substring)
 
static bool endsWith (const std::string &s, const std::string &substring)
 
static std::string & upperCase (std::string &s, size_t n=std::numeric_limits< size_t >::max())
 Turns n first characters uppercase. ASCII only. More...
 
static char upperCase (char c)
 Turns a character to uppercase. ASCII only.
 
static std::string & lowerCase (std::string &s, size_t n=std::numeric_limits< size_t >::max())
 Turns n first characters lowercase. ASCII only. More...
 
static char lowerCase (char c)
 Turns a character to lowercase. ASCII only.
 
static std::string replace (const std::string &src, const std::string &from, const std::string &to)
 
static void replace (const conv_map_t &m, std::string &s, std::size_t pos=0)
 Replaces instances appearing as map keys to map values. More...
 
static void replace (const std::string &src, const std::string &from, const std::string &to, std::string &dst)
 In src, replaces instances of 'from' to 'to', storing the result in dst.
 
static std::string trim (const std::string &s, const std::string &trimChars=" \t\n\r")
 Returns a string without leading and trailing whitespace (or str undesired chars).
 
static std::string trimSymmetric (const std::string &s, const std::string &leading="'\"", const std::string &trailing="")
 Coupled trimming: remove a single leading and trailing char, if both found. More...
 
static bool trimScan (const std::string &s, size_t &pos1, size_t &pos2, const std::string &trimChars=" \t\n")
 Returns indices to trimmed segment of s such that it can be extracted as s.substr(pos1, pos2-pos1). More...
 
template<class T , class C >
static void split (const std::string &s, T &sequence, const C &separators, const std::string &trimChars=" \t\n")
 Splits and trims a given std::string to a std Sequence. More...
 
template<class T1 , class T2 , class C >
static bool split2 (const std::string &s, T1 &first, T2 &second, const C &separators, const std::string &trimChars=" \t\n")
 Splits and trims a given std::string to a std Sequence. More...
 
template<class T >
static std::ostream & join (const T &container, std::ostream &ostr, char separator=0)
 Writes a STL Container (list, vector, set) to a stream, using an optional separator char (e.g. ','). More...
 
template<class T >
static std::string join (T &container, char separator=0)
 Writes a STL Container (list, vector, set) to a string, using an optional separator char (like ','). More...
 
template<unsigned int S>
static void read (std::istream &istr, std::string &s)
 Read input stream and append it to s.
 
template<class T >
static void convert (const std::string &s, T &dst)
 Conversion from std::string to basic types, including std::string. More...
 
template<class T >
static T convert (const std::string &s)
 Conversion from std::string to basic types, including std::string. More...
 
template<class T >
static std::string & import (const T &src, std::string &target)
 
template<class T >
static const T & lazyConvert (const std::string &s, T &tmp)
 Convert only if needed. If T is std::string, returns s directly. More...
 

Member Function Documentation

◆ convert() [1/2]

T convert ( const std::string &  s)
inlinestatic

Conversion from std::string to basic types, including std::string.

This version is slow for string targets (T).

Parameters
str- input string
Returns
- converted value.

◆ convert() [2/2]

void convert ( const std::string &  s,
T &  dst 
)
inlinestatic

Conversion from std::string to basic types, including std::string.

This version is slow for string targets (T).

Parameters
str- input string
dst- destination
Returns
- reference to the result of the conversion.

◆ join() [1/2]

static std::ostream& join ( const T &  container,
std::ostream &  ostr,
char  separator = 0 
)
inlinestatic

Writes a STL Container (list, vector, set) to a stream, using an optional separator char (e.g. ',').

See also
StringTools::split()

◆ join() [2/2]

static std::string join ( T &  container,
char  separator = 0 
)
inlinestatic

Writes a STL Container (list, vector, set) to a string, using an optional separator char (like ',').

See also
StringTools::split()

◆ lazyConvert()

const T & lazyConvert ( const std::string &  s,
T &  tmp 
)
inlinestatic

Convert only if needed. If T is std::string, returns s directly.

Parameters
str- input string
tmp- "hidden" temporary value; returned reference should be read instead of this.
Template Parameters
-string or any class that does not clear the object upon identity assigment (obj=obj).
Returns
- reference to the result of the conversion.

Note: the classes of target objects first and second should have an assignment operator=() that does not clear the object when assigning itself obj=obj !

◆ lowerCase()

std::string & lowerCase ( std::string &  s,
size_t  n = std::numeric_limits<size_t>::max() 
)
static

Turns n first characters lowercase. ASCII only.

Turns n first characters lowercase. Ascii only.

◆ replace() [1/2]

void replace ( const conv_map_t &  m,
std::string &  s,
std::size_t  pos = 0 
)
static

Replaces instances appearing as map keys to map values.

See also
RegExp::replace.

◆ replace() [2/2]

static std::string replace ( const std::string &  src,
const std::string &  from,
const std::string &  to 
)
inlinestatic

Replaces instances of 'from' to 'to' in src, storing the result in dst. In src, replaces instances of 'from' to 'to', returning the result. Safe. Uses temporary std::string.

See also
RegExp::replace.

◆ split()

void split ( const std::string &  s,
T &  sequence,
const C &  separators,
const std::string &  trimChars = " \t\n" 
)
static

Splits and trims a given std::string to a std Sequence.

Parameters
s– string to be chopped
sequence– target sequence; must implement end() and insert() operators.
separators– single char, array or string
trimChars– single char, array or string

Assumes that T::operator=(std::string) exists.

Given an empty std::string, returns a sequence containing an empty std::string. Hence, does not return an empty sequence.

Given a null or empty separator, returns a single-element sequence containing the original string.

◆ split2()

static bool split2 ( const std::string &  s,
T1 &  first,
T2 &  second,
const C &  separators,
const std::string &  trimChars = " \t\n" 
)
static

Splits and trims a given std::string to a std Sequence.

Splits and trims a given std::string to two substrings, by first separator encountered.

Template Parameters
T1- target of 1st part ("key")
T2- target of 1st part ("value")
C- character or string (any char in which will split)
Parameters
s- source string
first- target of 1st part, typically the "key"
second- target of 2nd part, typically the "object"

Given an empty std::string, returns empty (intact) destinations. Given a string that does contain a separator, assigns the string on first , leaving empty/intact.

Note: the classes of target objects first and second should have an assignment operator=() that does not clear the object when assigning itself obj=obj !

◆ trimScan()

bool trimScan ( const std::string &  s,
size_t &  pos1,
size_t &  pos2,
const std::string &  trimChars = " \t\n" 
)
static

Returns indices to trimmed segment of s such that it can be extracted as s.substr(pos1, pos2-pos1).

Starting from pos1, searches for the first segment not containing trimChars.

Parameters
s- string to be scanned
pos1- before: index of 1st char to scan right, after: index of 1st char of the segment, if found; else s.size().
pos2- before: index of 1st char to scan left, after: index of the char after the segment, or s.size().
Returns
- true if a non-empty segment found.
See also
trim(const std::string &, const std::string &)

◆ trimSymmetric()

std::string trimSymmetric ( const std::string &  s,
const std::string &  leading = "'\"",
const std::string &  trailing = "" 
)
static

Coupled trimming: remove a single leading and trailing char, if both found.

s = trim("\"test\"");
s = trim("{test}", "{", "}");
static std::string trim(const std::string &s, const std::string &trimChars=" \t\n\r")
Returns a string without leading and trailing whitespace (or str undesired chars).
Definition: String.cpp:137

◆ upperCase()

std::string & upperCase ( std::string &  s,
size_t  n = std::numeric_limits<size_t>::max() 
)
static

Turns n first characters uppercase. ASCII only.

Turns n first characters uppercase. Ascii only.


The documentation for this class was generated from the following files: