CppCommon  1.0.4.1
C++ Common Library
Public Member Functions | Static Public Member Functions | List of all members
CppCommon::StringUtils Class Reference

String utilities. More...

#include <string_utils.h>

Public Member Functions

 StringUtils ()=delete
 
 StringUtils (const StringUtils &)=delete
 
 StringUtils (StringUtils &&)=delete
 
 ~StringUtils ()=delete
 
StringUtilsoperator= (const StringUtils &)=delete
 
StringUtilsoperator= (StringUtils &&)=delete
 
template<>
std::string ToString (const int8_t &value)
 
template<>
std::string ToString (const uint8_t &value)
 
template<>
int8_t FromString (std::string_view str)
 

Static Public Member Functions

static bool IsBlank (char ch)
 Is the given character blank (empty or contains only space characters)? More...
 
static bool IsBlank (const char *str)
 Is the given C-string blank (empty or contains only space characters)? More...
 
static bool IsBlank (std::string_view str)
 Is the given string blank (empty or contains only space characters)? More...
 
static bool IsPatternMatch (const std::string &patterns, const std::string &str)
 Is the given string match to the given patterns? More...
 
static char ToLower (char ch)
 Convert the given character to lower case. More...
 
static char ToUpper (char ch)
 Convert the given character to UPPER case. More...
 
static std::string ToLower (std::string_view str)
 Convert the given constant string converted to lower case. More...
 
static std::string ToUpper (std::string_view str)
 Convert the the given constant string converted to UPPER case. More...
 
static std::string & Lower (std::string &str)
 Convert the given string to lower case. More...
 
static std::string & Upper (std::string &str)
 Convert the given string to UPPER case. More...
 
static std::string ToLTrim (std::string_view str)
 Trims space characters from the start of the given constant string. More...
 
static std::string ToRTrim (std::string_view str)
 Trims space characters from the end of the given constant string. More...
 
static std::string ToTrim (std::string_view str)
 Trims space characters from the both sides of the given constant string. More...
 
static std::string & LTrim (std::string &str)
 Trims space characters from the start of the given string. More...
 
static std::string & RTrim (std::string &str)
 Trims space characters from the end of the given string. More...
 
static std::string & Trim (std::string &str)
 Trims space characters from the both sides of the given string. More...
 
static std::string RemoveBlank (std::string_view str)
 Remove blank characters from the given string. More...
 
static std::string & RemoveBlank (std::string &str)
 Remove blank characters from the given string. More...
 
static bool Compare (std::string_view str1, std::string_view str2)
 Compare two strings case sensitive version. More...
 
static bool CompareNoCase (std::string_view str1, std::string_view str2)
 Compare two strings case insensitive version. More...
 
static bool Contains (std::string_view str, const char ch)
 Is the given string contains the given character? More...
 
static bool Contains (std::string_view str, const char *substr)
 Is the given string contains the given C-string? More...
 
static bool Contains (std::string_view str, std::string_view substr)
 Is the given string contains the given substring? More...
 
static size_t CountAll (std::string_view str, std::string_view substr)
 Count all occurrences of substring. More...
 
static bool ReplaceFirst (std::string &str, std::string_view substr, std::string_view with)
 Replace the first occurrence of substring with another substring. More...
 
static bool ReplaceLast (std::string &str, std::string_view substr, std::string_view with)
 Replace the last occurrence of substring with another substring. More...
 
static bool ReplaceAll (std::string &str, std::string_view substr, std::string_view with)
 Replace all occurrences of substring with another substring. More...
 
static bool StartsWith (std::string_view str, std::string_view prefix)
 Checks the given string for specific prefix. More...
 
static bool EndsWith (std::string_view str, std::string_view suffix)
 Checks the given string for specific suffix. More...
 
static std::vector< std::string > Split (std::string_view str, char delimiter, bool skip_empty=false)
 Split the string into tokens by the given delimiter character. More...
 
static std::vector< std::string > Split (std::string_view str, std::string_view delimiter, bool skip_empty=false)
 Split the string into tokens by the given delimiter string. More...
 
static std::vector< std::string > SplitByAny (std::string_view str, std::string_view delimiters, bool skip_empty=false)
 Split the string into tokens by the any character in the given delimiter string. More...
 
static std::string Join (const std::vector< std::string > &tokens, bool skip_empty=false, bool skip_blank=false)
 Join tokens into the string. More...
 
static std::string Join (const std::vector< std::string > &tokens, char delimiter, bool skip_empty=false, bool skip_blank=false)
 Join tokens into the string with delimiter character. More...
 
static std::string Join (const std::vector< std::string > &tokens, const char *delimiter, bool skip_empty=false, bool skip_blank=false)
 Join tokens into the string with delimiter C-string. More...
 
static std::string Join (const std::vector< std::string > &tokens, std::string_view delimiter, bool skip_empty=false, bool skip_blank=false)
 Join tokens into the string with delimiter string. More...
 
template<typename T >
static std::string ToString (const T &value)
 Converts arbitrary datatypes into string using std::ostringstream. More...
 
template<typename T >
static T FromString (std::string_view str)
 Converts strings to arbitrary datatypes using std::istringstream. More...
 

Detailed Description

String utilities.

String utilities contains methods for UPPER/lower case conversions, join/split strings and other useful string manipulation methods.

Thread-safe.

Definition at line 28 of file string_utils.h.

Constructor & Destructor Documentation

◆ StringUtils() [1/3]

CppCommon::StringUtils::StringUtils ( )
delete

◆ StringUtils() [2/3]

CppCommon::StringUtils::StringUtils ( const StringUtils )
delete

◆ StringUtils() [3/3]

CppCommon::StringUtils::StringUtils ( StringUtils &&  )
delete

◆ ~StringUtils()

CppCommon::StringUtils::~StringUtils ( )
delete

Member Function Documentation

◆ Compare()

bool CppCommon::StringUtils::Compare ( std::string_view  str1,
std::string_view  str2 
)
static

Compare two strings case sensitive version.

Parameters
str1- First string to compare
str2- Second string to compare
Returns
'true' if two strings are equal, 'false' if two strings are different

Definition at line 92 of file string_utils.cpp.

◆ CompareNoCase()

bool CppCommon::StringUtils::CompareNoCase ( std::string_view  str1,
std::string_view  str2 
)
static

Compare two strings case insensitive version.

Parameters
str1- First string to compare
str2- Second string to compare
Returns
'true' if two strings are equal, 'false' if two strings are different

Definition at line 97 of file string_utils.cpp.

◆ Contains() [1/3]

bool CppCommon::StringUtils::Contains ( std::string_view  str,
const char *  substr 
)
inlinestatic

Is the given string contains the given C-string?

Parameters
str- String to search in
substr- Substring to find
Returns
'true' if the substring was found, 'false' if the substring was not found

Definition at line 90 of file string_utils.inl.

◆ Contains() [2/3]

bool CppCommon::StringUtils::Contains ( std::string_view  str,
const char  ch 
)
inlinestatic

Is the given string contains the given character?

Parameters
str- String to search in
ch- Character to find
Returns
'true' if the substring was found, 'false' if the substring was not found
Examples
string_utils.cpp.

Definition at line 85 of file string_utils.inl.

◆ Contains() [3/3]

bool CppCommon::StringUtils::Contains ( std::string_view  str,
std::string_view  substr 
)
inlinestatic

Is the given string contains the given substring?

Parameters
str- String to search in
substr- Substring to find
Returns
'true' if the substring was found, 'false' if the substring was not found

Definition at line 95 of file string_utils.inl.

◆ CountAll()

size_t CppCommon::StringUtils::CountAll ( std::string_view  str,
std::string_view  substr 
)
static

Count all occurrences of substring.

Parameters
str- Modifying string
substr- Substring to find
Returns
Count of all substring occurrences
Examples
string_utils.cpp.

Definition at line 104 of file string_utils.cpp.

◆ EndsWith()

bool CppCommon::StringUtils::EndsWith ( std::string_view  str,
std::string_view  suffix 
)
inlinestatic

Checks the given string for specific suffix.

Parameters
str- String to check
suffix- Suffix to check
Returns
'true' if the given string ends with the specific suffix, 'false' if the given string does not end with the specific suffix

Definition at line 105 of file string_utils.inl.

◆ FromString() [1/2]

template<typename T >
T CppCommon::StringUtils::FromString ( std::string_view  str)
inlinestatic

Converts strings to arbitrary datatypes using std::istringstream.

Parameters
str- String converted into the value
Returns
Result converted value

Definition at line 135 of file string_utils.inl.

◆ FromString() [2/2]

template<>
bool CppCommon::StringUtils::FromString ( std::string_view  str)
inline

Definition at line 143 of file string_utils.inl.

◆ IsBlank() [1/3]

bool CppCommon::StringUtils::IsBlank ( char  ch)
inlinestatic

Is the given character blank (empty or contains only space characters)?

Parameters
ch- Character to check
Returns
'true' if given character is blank, 'false' if given character is not blank

Definition at line 16 of file string_utils.inl.

◆ IsBlank() [2/3]

bool CppCommon::StringUtils::IsBlank ( const char *  str)
static

Is the given C-string blank (empty or contains only space characters)?

Parameters
str- C-string to check
Returns
'true' if given C-string is blank, 'false' if given C-string is not blank

Definition at line 16 of file string_utils.cpp.

◆ IsBlank() [3/3]

bool CppCommon::StringUtils::IsBlank ( std::string_view  str)
static

Is the given string blank (empty or contains only space characters)?

Parameters
str- String to check
Returns
'true' if given string is blank, 'false' if given string is not blank

Definition at line 25 of file string_utils.cpp.

◆ IsPatternMatch()

bool CppCommon::StringUtils::IsPatternMatch ( const std::string &  patterns,
const std::string &  str 
)
static

Is the given string match to the given patterns?

Patterns string contains one or more regular expressions separated by ';'. If the regular expression has '!' prefix it treats as 'not matching'. Examples: "Demo.*;Live.*" + "DemoAccount" -> true "Demo.*;Live.*" + "LiveAccount" -> true "Demo.*;Live.*" + "UnknownAccount" -> false "!Demo.*;!Live.*" + "DemoAccount" -> false "!Demo.*;!Live.*" + "LiveAccount" -> false "!Demo.*;!Live.*" + "UnknownAccount" -> true

Parameters
patterns- Patterns to match with
str- String to match
Returns
'true' if given string matches, 'false' if given string does not match

Definition at line 37 of file string_utils.cpp.

◆ Join() [1/4]

std::string CppCommon::StringUtils::Join ( const std::vector< std::string > &  tokens,
bool  skip_empty = false,
bool  skip_blank = false 
)
static

Join tokens into the string.

Parameters
tokens- Vector of string tokens
skip_empty- Skip empty tokens flag (default is false)
skip_blank- Skip blank tokens flag (default is false)
Returns
Joined string
Examples
string_utils.cpp.

Definition at line 234 of file string_utils.cpp.

◆ Join() [2/4]

std::string CppCommon::StringUtils::Join ( const std::vector< std::string > &  tokens,
char  delimiter,
bool  skip_empty = false,
bool  skip_blank = false 
)
static

Join tokens into the string with delimiter character.

Parameters
tokens- Vector of string tokens
delimiter- Delimiter character
skip_empty- Skip empty tokens flag (default is false)
skip_blank- Skip blank tokens flag (default is false)
Returns
Joined string

Definition at line 248 of file string_utils.cpp.

◆ Join() [3/4]

std::string CppCommon::StringUtils::Join ( const std::vector< std::string > &  tokens,
const char *  delimiter,
bool  skip_empty = false,
bool  skip_blank = false 
)
static

Join tokens into the string with delimiter C-string.

Parameters
tokens- Vector of string tokens
delimiter- Delimiter C-string
skip_empty- Skip empty tokens flag (default is false)
skip_blank- Skip blank tokens flag (default is false)
Returns
Joined string

Definition at line 265 of file string_utils.cpp.

◆ Join() [4/4]

std::string CppCommon::StringUtils::Join ( const std::vector< std::string > &  tokens,
std::string_view  delimiter,
bool  skip_empty = false,
bool  skip_blank = false 
)
static

Join tokens into the string with delimiter string.

Parameters
tokens- Vector of string tokens
delimiter- Delimiter string
skip_empty- Skip empty tokens flag (default is false)
skip_blank- Skip blank tokens flag (default is false)
Returns
Joined string

Definition at line 282 of file string_utils.cpp.

◆ Lower()

std::string & CppCommon::StringUtils::Lower ( std::string &  str)
inlinestatic

Convert the given string to lower case.

Parameters
str- String to convert
Returns
The same converted string

Definition at line 55 of file string_utils.inl.

◆ LTrim()

std::string & CppCommon::StringUtils::LTrim ( std::string &  str)
static

Trims space characters from the start of the given string.

Parameters
str- String to trim
Returns
The same trimmed string

Definition at line 80 of file string_utils.cpp.

◆ operator=() [1/2]

StringUtils& CppCommon::StringUtils::operator= ( const StringUtils )
delete

◆ operator=() [2/2]

StringUtils& CppCommon::StringUtils::operator= ( StringUtils &&  )
delete

◆ RemoveBlank() [1/2]

std::string & CppCommon::StringUtils::RemoveBlank ( std::string &  str)
inlinestatic

Remove blank characters from the given string.

Parameters
str- String to remove blank characters
Returns
The same string with removed blank characters

Definition at line 79 of file string_utils.inl.

◆ RemoveBlank() [2/2]

std::string CppCommon::StringUtils::RemoveBlank ( std::string_view  str)
inlinestatic

Remove blank characters from the given string.

Parameters
str- String to remove blank characters
Returns
String with removed blank characters
Examples
string_utils.cpp.

Definition at line 72 of file string_utils.inl.

◆ ReplaceAll()

bool CppCommon::StringUtils::ReplaceAll ( std::string &  str,
std::string_view  substr,
std::string_view  with 
)
static

Replace all occurrences of substring with another substring.

Parameters
str- Modifying string
substr- Substring to find
with- Substring to replace
Returns
'true' if all occurrences of substring were found and relapsed, 'false' if the substring was not found
Examples
string_utils.cpp.

Definition at line 138 of file string_utils.cpp.

◆ ReplaceFirst()

bool CppCommon::StringUtils::ReplaceFirst ( std::string &  str,
std::string_view  substr,
std::string_view  with 
)
static

Replace the first occurrence of substring with another substring.

Parameters
str- Modifying string
substr- Substring to find
with- Substring to replace
Returns
'true' if first occurrence of substring was found and relapsed, 'false' if the substring was not found
Examples
string_utils.cpp.

Definition at line 118 of file string_utils.cpp.

◆ ReplaceLast()

bool CppCommon::StringUtils::ReplaceLast ( std::string &  str,
std::string_view  substr,
std::string_view  with 
)
static

Replace the last occurrence of substring with another substring.

Parameters
str- Modifying string
substr- Substring to find
with- Substring to replace
Returns
'true' if last occurrence of substring was found and relapsed, 'false' if the substring was not found
Examples
string_utils.cpp.

Definition at line 128 of file string_utils.cpp.

◆ RTrim()

std::string & CppCommon::StringUtils::RTrim ( std::string &  str)
static

Trims space characters from the end of the given string.

Parameters
str- String to trim
Returns
The same trimmed string

Definition at line 86 of file string_utils.cpp.

◆ Split() [1/2]

std::vector< std::string > CppCommon::StringUtils::Split ( std::string_view  str,
char  delimiter,
bool  skip_empty = false 
)
static

Split the string into tokens by the given delimiter character.

Parameters
str- String to split
delimiter- Delimiter character
skip_empty- Skip empty substrings flag (default is false)
Returns
Vector of tokens
Examples
string_utils.cpp.

Definition at line 153 of file string_utils.cpp.

◆ Split() [2/2]

std::vector< std::string > CppCommon::StringUtils::Split ( std::string_view  str,
std::string_view  delimiter,
bool  skip_empty = false 
)
static

Split the string into tokens by the given delimiter string.

Parameters
str- String to split
delimiter- Delimiter string
skip_empty- Skip empty substrings flag (default is false)
Returns
Vector of tokens

Definition at line 180 of file string_utils.cpp.

◆ SplitByAny()

std::vector< std::string > CppCommon::StringUtils::SplitByAny ( std::string_view  str,
std::string_view  delimiters,
bool  skip_empty = false 
)
static

Split the string into tokens by the any character in the given delimiter string.

Parameters
str- String to split
delimiters- Delimiters string
skip_empty- Skip empty substrings flag (default is false)
Returns
Vector of string tokens

Definition at line 207 of file string_utils.cpp.

◆ StartsWith()

bool CppCommon::StringUtils::StartsWith ( std::string_view  str,
std::string_view  prefix 
)
inlinestatic

Checks the given string for specific prefix.

Parameters
str- String to check
prefix- Prefix to check
Returns
'true' if the given string starts with the specific prefix, 'false' if the given string does not start with the specific prefix

Definition at line 100 of file string_utils.inl.

◆ ToLower() [1/2]

char CppCommon::StringUtils::ToLower ( char  ch)
inlinestatic

Convert the given character to lower case.

Parameters
ch- Character to convert
Examples
string_utils.cpp.

Definition at line 26 of file string_utils.inl.

◆ ToLower() [2/2]

std::string CppCommon::StringUtils::ToLower ( std::string_view  str)
inlinestatic

Convert the given constant string converted to lower case.

Parameters
str- String to convert
Returns
String converted to lower case

Definition at line 41 of file string_utils.inl.

◆ ToLTrim()

std::string CppCommon::StringUtils::ToLTrim ( std::string_view  str)
static

Trims space characters from the start of the given constant string.

Parameters
str- String to trim
Returns
Trimmed string
Examples
string_utils.cpp.

Definition at line 62 of file string_utils.cpp.

◆ ToRTrim()

std::string CppCommon::StringUtils::ToRTrim ( std::string_view  str)
static

Trims space characters from the end of the given constant string.

Parameters
str- String to trim
Returns
Trimmed string
Examples
string_utils.cpp.

Definition at line 67 of file string_utils.cpp.

◆ ToString() [1/3]

template<>
std::string CppCommon::StringUtils::ToString ( const int8_t &  value)
inline

Definition at line 119 of file string_utils.inl.

◆ ToString() [2/3]

template<typename T >
std::string CppCommon::StringUtils::ToString ( const T &  value)
inlinestatic

Converts arbitrary datatypes into string using std::ostringstream.

Parameters
value- Value to convert
Returns
Result converted string
Examples
string_utils.cpp.

Definition at line 111 of file string_utils.inl.

◆ ToString() [3/3]

template<>
std::string CppCommon::StringUtils::ToString ( const uint8_t &  value)
inline

Definition at line 127 of file string_utils.inl.

◆ ToTrim()

std::string CppCommon::StringUtils::ToTrim ( std::string_view  str)
static

Trims space characters from the both sides of the given constant string.

Parameters
str- String to trim
Returns
Trimmed string
Examples
string_utils.cpp.

Definition at line 72 of file string_utils.cpp.

◆ ToUpper() [1/2]

char CppCommon::StringUtils::ToUpper ( char  ch)
inlinestatic

Convert the given character to UPPER case.

Parameters
ch- Character to convert
Examples
string_utils.cpp.

Definition at line 36 of file string_utils.inl.

◆ ToUpper() [2/2]

std::string CppCommon::StringUtils::ToUpper ( std::string_view  str)
inlinestatic

Convert the the given constant string converted to UPPER case.

Parameters
str- String to convert
Returns
String converted to UPPER case

Definition at line 48 of file string_utils.inl.

◆ Trim()

std::string & CppCommon::StringUtils::Trim ( std::string &  str)
inlinestatic

Trims space characters from the both sides of the given string.

Parameters
str- String to trim
Returns
The same trimmed string

Definition at line 67 of file string_utils.inl.

◆ Upper()

std::string & CppCommon::StringUtils::Upper ( std::string &  str)
inlinestatic

Convert the given string to UPPER case.

Parameters
str- String to convert
Returns
The same converted string

Definition at line 61 of file string_utils.inl.


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