Samchon Framework for CPP  1.0.0
samchon::library::StringUtil Class Reference

Utility class for string. More...

#include <StringUtil.hpp>

Static Public Member Functions

template<typename T , typename... _Args>
static auto substitute (const std::string &format, const T &val, const _Args &...args) -> std::string
 Substitutes "{n}" tokens within the specified string with the respective arguments passed in. More...
 
template<typename T , typename... _Args>
static auto substituteSQL (const std::string &format, const T &value, const _Args &...args) -> std::string
 Substitutes "{n}" tokens within the specified sql-string with the respective arguments passed in.
. More...
 
static auto isNumeric (const std::string &str) -> bool
 Returns wheter the std::string represents Number or not
. More...
 
static auto toNumber (const std::string &str) -> double
 Number std::string to Number having ',' symbols. More...
 
static auto numberFormat (double val, int precision=2) -> std::string
 
static auto percentFormat (double val, int precision=2) -> std::string
 Returns a percentage string converted from the number rounded off from specified precision with "," symbols
     ex) percentFormat(11.3391, 1) => 1,133.9%. More...
 
static auto colorNumberFormat (double value, int precision=2, double delimiter=0.0) -> std::string
 Returns a string converted from the number rounded off from specified precision with "," symbols and color tag
     ex) numberFormat(17151.339, 2) => <font color="red">17,151.34</font> More...
 
static auto colorPercentFormat (double value, int precision=2, double delimiter=0.0) -> std::string
 Returns a percentage string converted from the number rounded off from specified precision with "," symbols
     ex) percentFormat(11.3391, 1) => 1,133.9%. More...
 
static auto trim (const std::string &val, const std::vector< std::string > &delims) -> std::string
 Removes all designated characters from the beginning and end of the specified string. More...
 
static auto ltrim (const std::string &val, const std::vector< std::string > &delims) -> std::string
 Removes all designated characters from the beginning of the specified string. More...
 
static auto rtrim (const std::string &val, const std::vector< std::string > &delims) -> std::string
 Removes all designated characters from the end of the specified string. More...
 
static auto finds (const std::string &str, const std::vector< std::string > &delims, size_t startIndex=0) -> IndexPair< std::string >
 Finds first occurence in string. More...
 
static auto rfinds (const std::string &str, const std::vector< std::string > &delims, size_t endIndex=SIZE_MAX) -> IndexPair< std::string >
 Finds last occurence in string. More...
 
static auto substring (const std::string &str, size_t startIndex, size_t endIndex=SIZE_MAX) -> std::string
 Generates a substring. More...
 
static auto between (const std::string &str, const std::string &start="", const std::string &end="") -> std::string
 Generate a substring. More...
 
static auto addTab (const std::string &str, size_t n=1) -> std::string
 Adds tab() character to first position of each line. More...
 
static auto split (const std::string &str, const std::string &delim) -> std::vector< std::string >
 Generates substrings. More...
 
static auto betweens (const std::string &str, const std::string &start="", const std::string &end="") -> std::vector< std::string >
 Generates substrings. More...
 
static auto toLowerCase (const std::string &str) -> std::string
 Returns a string that all uppercase characters are converted to lowercase. More...
 
static auto yoUpperCase (const std::string &str) -> std::string
 
static auto replaceAll (const std::string &str, const std::string &before, const std::string &after) -> std::string
 Returns a string specified word is replaced. More...
 
static auto replaceAll (const std::string &str, const std::vector< std::pair< std::string, std::string >> &pairs) -> std::string
 Returns a string specified words are replaced. More...
 
static auto removeHTMLSpaces (const std::string &str) -> std::string
 Replace all HTML spaces to a literal space. More...
 

Detailed Description

Utility class for string.

StringUtil is an utility class providing lots of static methods for std::string.

There are two methods to strength std::string to have addictional uility methods like trim and split. The way of first is to make std::string class inheriting from std::string. The second is to make StringUtil class having static methods.

But those methods have problems. std::string class violates standard and StringUtil class violates principle of Object-Oriented Design. For the present, I've made the StringUtil class, but if you have a good opinion about the issue, please write your opinion on my github.

library_string.png
See also
library::WeakString
samchon::library
Author
Jeongho Nam http://samchon.org

Definition at line 36 of file StringUtil.hpp.

Member Function Documentation

template<typename T , typename... _Args>
static auto samchon::library::StringUtil::substitute ( const std::string &  format,
const T &  val,
const _Args &...  args 
) -> std::string
inlinestatic

Substitutes "{n}" tokens within the specified string with the respective arguments passed in.

Parameters
formatThe string to make substitutions in.
This string can contain special tokens of the form {n}, where n is a zero based index, that will be replaced with the additional parameters found at that index if specified
valTarget value to substitute the minimum {n} tokens
argsAdditional parameters that can be substituted in the str parameter at each {n} location, where n is an integer (zero based) index value into the varadics of values specified.
Returns
New string with all of the {n} tokens replaced with the respective arguments specified.

Definition at line 54 of file StringUtil.hpp.

Referenced by colorNumberFormat(), colorPercentFormat(), samchon::protocol::WebServerConnector::connect(), samchon::library::SQLi::connect(), samchon::library::HTTPLoader::load(), samchon::examples::packer::Instance::toString(), samchon::examples::tsp::GeometryPoint::toString(), samchon::library::Date::toString(), and samchon::protocol::WebServer::WebServer().

Here is the caller graph for this function:

template<typename T , typename... _Args>
static auto samchon::library::StringUtil::substituteSQL ( const std::string &  format,
const T &  value,
const _Args &...  args 
) -> std::string
inlinestatic

Substitutes "{n}" tokens within the specified sql-string with the respective arguments passed in.
.

Warning
substituteSQL creates the dynamic sql-statement.
Not recommended when the dynamic sql-statement is not only for procedure.
Parameters
formatThe string to make substitutions in.
This string can contain special tokens of the form {n}, where n is a zero based index, that will be replaced with the additional parameters found at that index if specified
valTarget value to substitute the minimum {n} tokens
argsAdditional parameters that can be substituted in the str parameter at each {n} location, where n is an integer (zero based) index value into the varadics of values specified.
Returns
New sql-string with all of the {n} tokens replaced with the respective arguments specified.

Definition at line 80 of file StringUtil.hpp.

References betweens(), samchon::WeakString::empty(), samchon::WeakString::find(), isNumeric(), samchon::library::Math::minimum(), replaceAll(), samchon::WeakString::replaceAll(), and samchon::WeakString::str().

Here is the call graph for this function:

static auto samchon::library::StringUtil::isNumeric ( const std::string &  str) -> bool
inlinestatic

Returns wheter the std::string represents Number or not
.

Parameters
strTarget std::string to check
Returns
Whether the std::string can be converted to Number or not

Definition at line 198 of file StringUtil.hpp.

Referenced by substituteSQL().

Here is the caller graph for this function:

static auto samchon::library::StringUtil::toNumber ( const std::string &  str) -> double
inlinestatic

Number std::string to Number having ',' symbols.

Parameters
strTarget std::string you want to convert to Number
Returns
Number from std::string

Definition at line 223 of file StringUtil.hpp.

References replaceAll().

Here is the call graph for this function:

static auto samchon::library::StringUtil::numberFormat ( double  val,
int  precision = 2 
) -> std::string
inlinestatic

Returns a string converted from the number rounded off from specified precision with "," symbols      ex) numberFormat(17151.339, 2) => 17,151.34

Parameters
valA number wants to convert to string
precisionTarget precision of roundoff
Returns
A string representing the number with roundoff and "," symbols

Definition at line 241 of file StringUtil.hpp.

Referenced by colorNumberFormat(), percentFormat(), and samchon::examples::tsp::Travel::toString().

Here is the caller graph for this function:

static auto samchon::library::StringUtil::percentFormat ( double  val,
int  precision = 2 
) -> std::string
inlinestatic

Returns a percentage string converted from the number rounded off from specified precision with "," symbols
     ex) percentFormat(11.3391, 1) => 1,133.9%.

Warning
Do not multiply by 100 to the value representing percent
Parameters
valA number wants to convert to percentage string
precisionTarget precision of roundoff

Definition at line 303 of file StringUtil.hpp.

References numberFormat().

Referenced by colorPercentFormat().

Here is the call graph for this function:

Here is the caller graph for this function:

static auto samchon::library::StringUtil::colorNumberFormat ( double  value,
int  precision = 2,
double  delimiter = 0.0 
) -> std::string
inlinestatic

Returns a string converted from the number rounded off from specified precision with "," symbols and color tag
     ex) numberFormat(17151.339, 2) => <font color="red">17,151.34</font>

Which color would be chosen

  • Number is positive, color is RED
  • Number is zero (0), color is BLACK
  • Number is negative, color is BLUE
Parameters
valA number wants to convert to colored string
precisionTarget precision of roundoff
Returns
A colored string representing the number with roundoff and "," symbols

Definition at line 325 of file StringUtil.hpp.

References numberFormat(), and substitute().

Here is the call graph for this function:

static auto samchon::library::StringUtil::colorPercentFormat ( double  value,
int  precision = 2,
double  delimiter = 0.0 
) -> std::string
inlinestatic

Returns a percentage string converted from the number rounded off from specified precision with "," symbols
     ex) percentFormat(11.3391, 1) => 1,133.9%.

Warning
Do not multiply by 100 to the value representing percent
Parameters
valA number wants to convert to percentage string
precisionTarget precision of roundoff

Definition at line 349 of file StringUtil.hpp.

References percentFormat(), and substitute().

Here is the call graph for this function:

static auto samchon::library::StringUtil::trim ( const std::string &  val,
const std::vector< std::string > &  delims 
) -> std::string
inlinestatic

Removes all designated characters from the beginning and end of the specified string.

Parameters
strThe string should be trimmed
delimsDesignated character(s)
Returns
Updated string where designated characters was removed from the beginning and end

Definition at line 376 of file StringUtil.hpp.

Referenced by rtrim().

Here is the caller graph for this function:

static auto samchon::library::StringUtil::ltrim ( const std::string &  val,
const std::vector< std::string > &  delims 
) -> std::string
inlinestatic

Removes all designated characters from the beginning of the specified string.

Parameters
strThe string should be trimmed
delimsDesignated character(s)
Returns
Updated string where designated characters was removed from the beginning

Definition at line 388 of file StringUtil.hpp.

Referenced by rtrim().

Here is the caller graph for this function:

static auto samchon::library::StringUtil::rtrim ( const std::string &  val,
const std::vector< std::string > &  delims 
) -> std::string
inlinestatic

Removes all designated characters from the end of the specified string.

Parameters
strThe string should be trimmed
delimsDesignated character(s)
Returns
Updated string where designated characters was removed from the end

Definition at line 400 of file StringUtil.hpp.

References ltrim(), and trim().

Here is the call graph for this function:

static auto samchon::library::StringUtil::finds ( const std::string &  str,
const std::vector< std::string > &  delims,
size_t  startIndex = 0 
) -> IndexPair<std::string>
inlinestatic

Finds first occurence in string.

Finds first occurence position of each delim in the string after startIndex and returns the minimum position of them

If startIndex is not specified, then starts from 0.
If failed to find any substring, returns -1 (std::string::npos)

Parameters
strTarget string to find
delimsThe substrings of target(str) which to find
startIndexSpecified starting index of find. Default is 0
Returns
pair<size_t := position, string := matched substring>

Definition at line 449 of file StringUtil.hpp.

References samchon::WeakString::finds(), samchon::IndexPair< T >::get_index(), and samchon::IndexPair< T >::getValue().

Here is the call graph for this function:

static auto samchon::library::StringUtil::rfinds ( const std::string &  str,
const std::vector< std::string > &  delims,
size_t  endIndex = SIZE_MAX 
) -> IndexPair<std::string>
inlinestatic

Finds last occurence in string.

Finds last occurence position of each delim in the string before endIndex and returns the maximum position of them

If index is not specified, then starts str.size() - 1
If failed to find any substring, returns -1 (std::string::npos)

Parameters
strTarget string to find
delimsThe substrings of target(str) which to find
endIndexSpecified starting index of find. Default is str.size() - 1
Returns
pair<size_t := position, string := matched substring>

Definition at line 472 of file StringUtil.hpp.

References samchon::IndexPair< T >::get_index(), samchon::IndexPair< T >::getValue(), and samchon::WeakString::rfinds().

Here is the call graph for this function:

static auto samchon::library::StringUtil::substring ( const std::string &  str,
size_t  startIndex,
size_t  endIndex = SIZE_MAX 
) -> std::string
inlinestatic

Generates a substring.

Extracts a string consisting of the character specified by startIndex and all characters up to endIndex - 1 If endIndex is not specified, string::size() will be used instead.
If endIndex is greater than startIndex, then those will be swapped

Parameters
strTarget string to be applied substring
startIndexIndex of the first character.
If startIndex is greater than endIndex, those will be swapped
endIndexIndex of the last character - 1.
If not specified, then string::size() will be used instead
Returns
Extracted string by specified index(es)

Definition at line 495 of file StringUtil.hpp.

References samchon::WeakString::substring().

Here is the call graph for this function:

static auto samchon::library::StringUtil::between ( const std::string &  str,
const std::string &  start = "",
const std::string &  end = "" 
) -> std::string
inlinestatic

Generate a substring.

Extracts a substring consisting of the characters from specified start to end It's same with str.substring( ? = (str.find(start) + start.size()), str.find(end, ?) )

ex) between("ABCD[EFGH]IJK", "[", "]") => "EFGH"

  • If start is not specified, extracts from begin of the string to end.
  • If end is not specified, extracts from start to end of the string.
  • If start and end are all omitted, returns str, itself.
Parameters
strTarget string to be applied between
startA string for separating substring at the front
endA string for separating substring at the end
Returns
substring by specified terms

Definition at line 520 of file StringUtil.hpp.

References samchon::WeakString::between().

Here is the call graph for this function:

static auto samchon::library::StringUtil::addTab ( const std::string &  str,
size_t  n = 1 
) -> std::string
inlinestatic

Adds tab() character to first position of each line.

Parameters
strTarget str to add tabs
nThe size of tab to be added for each line
Returns
A string added multiple tabs

Definition at line 534 of file StringUtil.hpp.

References split().

Here is the call graph for this function:

static auto samchon::library::StringUtil::split ( const std::string &  str,
const std::string &  delim 
) -> std::vector<std::string>
inlinestatic

Generates substrings.

Splits a string into an array of substrings by dividing the specified delimiter

Parameters
strTarget string to split
delimThe pattern that specifies where to split this string
Returns
An array of substrings

Definition at line 563 of file StringUtil.hpp.

References betweens(), and samchon::WeakString::split().

Referenced by addTab().

Here is the call graph for this function:

Here is the caller graph for this function:

static auto samchon::library::StringUtil::betweens ( const std::string &  str,
const std::string &  start = "",
const std::string &  end = "" 
) -> std::vector<std::string>
inlinestatic

Generates substrings.

Splits a string into an array of substrings dividing by specified delimeters of start and end. It's the array of substrings adjusted the between.

  • If startStr is omitted, it's same with the split by endStr not having last item
  • If endStr is omitted, it's same with the split by startStr not having first item
  • If startStr and endStar are all omitted, returns {str}
Parameters
strTarget string to split by between
startA string for separating substring at the front. If omitted, it's same with split(end) not having last item
endA string for separating substring at the end. If omitted, it's same with split(start) not having first item
Returns
An array of substrings

Definition at line 593 of file StringUtil.hpp.

References samchon::WeakString::betweens().

Referenced by split(), and substituteSQL().

Here is the call graph for this function:

Here is the caller graph for this function:

static auto samchon::library::StringUtil::toLowerCase ( const std::string &  str) -> std::string
inlinestatic

Returns a string that all uppercase characters are converted to lowercase.

Parameters
strTarget string to convert uppercase to lowercase
Returns
A string converted to lowercase

Definition at line 617 of file StringUtil.hpp.

References samchon::WeakString::toLowerCase().

Here is the call graph for this function:

static auto samchon::library::StringUtil::yoUpperCase ( const std::string &  str) -> std::string
inlinestatic

Returns a string all lowercase characters are converted to uppercase

Parameters
strTarget string to convert lowercase to uppercase
Returns
A string converted to uppercase

Definition at line 628 of file StringUtil.hpp.

References replaceAll(), and samchon::WeakString::yoUpperCase().

Here is the call graph for this function:

static auto samchon::library::StringUtil::replaceAll ( const std::string &  str,
const std::string &  before,
const std::string &  after 
) -> std::string
inlinestatic

Returns a string specified word is replaced.

Parameters
strTarget string to replace
beforeSpecific word you want to be replaced
afterSpecific word you want to replace
Returns
A string specified word is replaced

Definition at line 642 of file StringUtil.hpp.

References samchon::WeakString::replaceAll().

Referenced by removeHTMLSpaces(), substituteSQL(), toNumber(), and yoUpperCase().

Here is the call graph for this function:

Here is the caller graph for this function:

static auto samchon::library::StringUtil::replaceAll ( const std::string &  str,
const std::vector< std::pair< std::string, std::string >> &  pairs 
) -> std::string
inlinestatic

Returns a string specified words are replaced.

Parameters
strTarget string to replace
pairsA specific word's pairs you want to replace and to be replaced
Returns
A string specified words are replaced

Definition at line 657 of file StringUtil.hpp.

References samchon::WeakString::replaceAll().

Here is the call graph for this function:

static auto samchon::library::StringUtil::removeHTMLSpaces ( const std::string &  str) -> std::string
inlinestatic

Replace all HTML spaces to a literal space.

Parameters
strTarget string to replace.

Definition at line 668 of file StringUtil.hpp.

References replaceAll().

Here is the call graph for this function:


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