Devel Lib
Devel::StringUtils Namespace Reference

A namespace encapsulating a set of utility functions for string manipulation. More...

Functions

std::string Repeat (const std::string &i_sString, size_t i_nTimes)
 Repeats the input string a specified number of times. More...
 
std::vector< std::string > Split (const char *i_szString, size_t i_nLength, const char i_bDelimeter, const size_t i_nReserve=-1)
 Splits the input string into substrings based on the specified delimiter. More...
 
std::vector< std::string > Split (const std::string &i_sString, const char i_bDelimeter, const size_t i_nReserve=-1)
 Splits the input string into substrings based on the specified delimiter. More...
 
std::string Join (const std::vector< std::string > &i_asString, const std::string &i_sDelimeter)
 Joins the input vector of strings into a single string with the specified delimiter. More...
 
std::string Join (const std::vector< std::string > &i_asString, const char i_bDelimeter)
 
std::string & Replace (std::string &i_sSource, const char i_bFind, const char i_bReplaceWith)
 Replaces all instances of a specific character in the source string with another character. More...
 
std::string Replace (const std::string &i_sSource, const std::string &i_sFind, const std::string &i_sReplaceWith)
 Replaces all instances of a specific substring in the source string with another substring. More...
 
long long ToInt64 (const std::string &i_sBuffer)
 Converts a string to a 64-bit integer. More...
 
unsigned long long ToUInt64 (const std::string &i_sBuffer)
 Converts a string to an unsigned 64-bit integer. More...
 
int ToInt (const std::string &i_sBuffer)
 Converts a string to an integer. More...
 
unsigned int ToUInt (const std::string &i_sBuffer)
 Converts a string to an unsigned integer. More...
 
float ToFloat (const std::string &i_stBuffer)
 Converts a string to a float. More...
 
double ToDouble (const std::string &i_stBuffer)
 Converts a string to a double. More...
 
std::string ToLowerCopy (const std::string &i_sBuffer)
 Creates a copy of the string and converts it to lower case. More...
 
void ToLower (std::string &i_sBuffer)
 Converts the string to lower case. More...
 
std::string ToUpperCopy (const std::string &i_sBuffer)
 Creates a copy of the string and converts it to upper case. More...
 
void ToUpper (std::string &i_sBuffer)
 Converts the string to upper case. More...
 
bool Contains (const std::string &i_sString, const std::string &i_sToFind, const bool i_fCaseSensetive=true)
 Checks if the string contains the substring. More...
 
size_t Size (const char *i_szString, const size_t i_nMaxSize)
 Returns the size of the string, up to a maximum size. More...
 
void Set (char *i_pDestination, const char *i_szString, const size_t i_nSize, size_t i_nMax)
 Copies a string to a destination buffer, up to a maximum size. More...
 
void Set (char *i_pDestination, const char *i_szString, const size_t i_nMax)
 Copies a string to a destination buffer, up to a maximum size. More...
 
void Set (char *i_pDestination, const std::string &i_sData, const size_t i_nMax)
 Copies a string to a destination buffer, up to a maximum size. More...
 
std::string Get (char *i_pSource, const size_t i_nMaxSize)
 Returns a string copied from a source buffer, up to a maximum size. More...
 
size_t Count (const char *i_szString, const size_t i_nSize, const char i_bDelimeter)
 Counts the number of a specific character in the string. More...
 
size_t Count (const std::string &i_sString, const char i_bDelimeter)
 Counts the number of a specific character in the string. More...
 
template<typename T >
void ToArray (T &i_pDestination, const char *i_szString, const size_t i_nSize)
 Copies a string to a destination array, up to a maximum size. More...
 
template<typename T >
void ToArray (T &i_pDestination, const char *i_szString)
 Copies a string to a destination array. More...
 
template<typename T >
void ToArray (T &i_pDestination, const std::string &i_sData)
 Converts a string to an array. More...
 
template<typename T >
std::string FromArray (const T &i_pSource)
 Returns a string copied from a source array. More...
 
template<typename T , typename std::enable_if< std::is_arithmetic< T >::value >::type * = nullptr>
std::string ToHex (T i_oValue)
 Converts a numeric value to a hex string. More...
 

Detailed Description

A namespace encapsulating a set of utility functions for string manipulation.

These utility functions provide a set of operations for string manipulation such as splitting, joining, replacing, converting case, etc.

Example

In this example, we demonstrate the use of the StringUtils namespace.

std::string str = "Hello, World!";
// Repeat a string
std::string repeatedStr = Devel::StringUtils::Repeat(str, 3); // Returns "Hello, World!Hello, World!Hello, World!"
// Split a string
std::vector<std::string> splitStr = Devel::StringUtils::Split(str, ','); // Returns {"Hello", " World!"}
// Join a vector of strings
std::string joinedStr = Devel::StringUtils::Join(splitStr, ','); // Returns "Hello, World!"
// Replace character in a string
Devel::StringUtils::Replace(joinedStr, '!', '.'); // Modifies joinedStr to "Hello, World."
// Replace substring in a string
std::string replacedStr = Devel::StringUtils::Replace(joinedStr, "World", "There"); // Returns "Hello, There!"
// Convert string to int
int val = Devel::StringUtils::ToInt("123"); // Returns 123
// Convert string to float
float fval = Devel::StringUtils::ToFloat("123.456"); // Returns 123.456
// Convert string to lower case
std::string lowerStr = Devel::StringUtils::ToLowerCopy("HELLO"); // Returns "hello"
// Convert string to upper case
std::string upperStr = Devel::StringUtils::ToUpperCopy("hello"); // Returns "HELLO"
// Check if string contains a substring
bool contains = Devel::StringUtils::Contains(joinedStr, "Hello"); // Returns true
// Get string size
size_t size = Devel::StringUtils::Size(joinedStr.c_str(), 50); // Returns 13 (size of "Hello, World.")
// Set a string to a destination buffer
char buffer[50];
Devel::StringUtils::Set(buffer, joinedStr, 50); // Copies "Hello, World." to buffer
// Get a string from a source buffer
std::string bufferStr = Devel::StringUtils::Get(buffer, 50); // Returns "Hello, World."
// Count the number of a specific character in the string
size_t count = Devel::StringUtils::Count(joinedStr.c_str(), joinedStr.length(), 'l'); // Returns 3
uint8_t byteData = 12; // For example
std::string hexString = Devel::StringUtils::ToHex(byteData);
// Output will be "0C"
std::cout << hexString << std::endl;
char arr[] = "Example array";
size_t arrSize = sizeof(arr)/sizeof(char);
std::string str = Devel::StringUtils::FromArray(arr, arrSize);
// Output will be "Example array"
std::cout << str << std::endl;
std::string str = "Example string";
const size_t size = 20; // define size as needed
char arr[size] = {}; // initialize the array
// Output will be "Example string"
std::cout << arr << std::endl;
size_t Size(const char *i_szString, const size_t i_nMaxSize)
Returns the size of the string, up to a maximum size.
Definition: StringUtils.h:332
float ToFloat(const std::string &i_stBuffer)
Converts a string to a float.
Definition: StringUtils.h:256
std::string ToLowerCopy(const std::string &i_sBuffer)
Creates a copy of the string and converts it to lower case.
Definition: StringUtils.h:274
std::vector< std::string > Split(const char *i_szString, size_t i_nLength, const char i_bDelimeter, const size_t i_nReserve=-1)
Splits the input string into substrings based on the specified delimiter.
Definition: StringUtils.h:113
size_t Count(const char *i_szString, const size_t i_nSize, const char i_bDelimeter)
Counts the number of a specific character in the string.
Definition: StringUtils.h:392
std::string Repeat(const std::string &i_sString, size_t i_nTimes)
Repeats the input string a specified number of times.
Definition: StringUtils.h:95
std::string ToHex(T i_oValue)
Converts a numeric value to a hex string.
Definition: StringUtils.h:449
std::string & Replace(std::string &i_sSource, const char i_bFind, const char i_bReplaceWith)
Replaces all instances of a specific character in the source string with another character.
Definition: StringUtils.h:181
std::string Get(char *i_pSource, const size_t i_nMaxSize)
Returns a string copied from a source buffer, up to a maximum size.
Definition: StringUtils.h:381
std::string ToUpperCopy(const std::string &i_sBuffer)
Creates a copy of the string and converts it to upper case.
Definition: StringUtils.h:296
void ToArray(T &i_pDestination, const char *i_szString, const size_t i_nSize)
Copies a string to a destination array, up to a maximum size.
Definition: StringUtils.h:411
std::string Join(const std::vector< std::string > &i_asString, const std::string &i_sDelimeter)
Joins the input vector of strings into a single string with the specified delimiter.
Definition: StringUtils.h:152
void Set(char *i_pDestination, const char *i_szString, const size_t i_nSize, size_t i_nMax)
Copies a string to a destination buffer, up to a maximum size.
Definition: StringUtils.h:348
bool Contains(const std::string &i_sString, const std::string &i_sToFind, const bool i_fCaseSensetive=true)
Checks if the string contains the substring.
Definition: StringUtils.h:320
int ToInt(const std::string &i_sBuffer)
Converts a string to an integer.
Definition: StringUtils.h:239
std::string FromArray(const T &i_pSource)
Returns a string copied from a source array.
Definition: StringUtils.h:439

Function Documentation

◆ Contains()

bool Devel::StringUtils::Contains ( const std::string &  i_sString,
const std::string &  i_sToFind,
const bool  i_fCaseSensetive = true 
)
inline

Checks if the string contains the substring.

Parameters
i_sStringThe string to check.
i_sToFindThe substring to find.
i_fCaseSensetiveWhether the search is case sensitive.
Returns
True if the string contains the substring, false otherwise.

◆ Count() [1/2]

size_t Devel::StringUtils::Count ( const char *  i_szString,
const size_t  i_nSize,
const char  i_bDelimeter 
)
inline

Counts the number of a specific character in the string.

Parameters
i_szStringThe string to search.
i_nSizeThe size of the string.
i_bDelimeterThe character to count.
Returns
The count of the character in the string.

◆ Count() [2/2]

size_t Devel::StringUtils::Count ( const std::string &  i_sString,
const char  i_bDelimeter 
)
inline

Counts the number of a specific character in the string.

Parameters
i_sStringThe string to search.
i_bDelimeterThe character to count.
Returns
The count of the character in the string.

◆ FromArray()

template<typename T >
std::string Devel::StringUtils::FromArray ( const T &  i_pSource)

Returns a string copied from a source array.

Parameters
i_pSourceThe source array.
Returns
The copied string.

◆ Get()

std::string Devel::StringUtils::Get ( char *  i_pSource,
const size_t  i_nMaxSize 
)
inline

Returns a string copied from a source buffer, up to a maximum size.

Parameters
i_pSourceThe source buffer.
i_nMaxSizeThe maximum size.
Returns
The copied string.

◆ Join() [1/2]

std::string Devel::StringUtils::Join ( const std::vector< std::string > &  i_asString,
const char  i_bDelimeter 
)
inline
Parameters
i_asStringThe vector of strings to join.
i_bDelimeterThe character to use for joining the strings.
Returns
A string joined from the input vector of strings with the specified delimiter.

◆ Join() [2/2]

std::string Devel::StringUtils::Join ( const std::vector< std::string > &  i_asString,
const std::string &  i_sDelimeter 
)
inline

Joins the input vector of strings into a single string with the specified delimiter.

Parameters
i_asStringThe vector of strings to join.
i_sDelimeterThe delimiter to use for joining the strings.
Returns
A string joined from the input vector of strings with the specified delimiter.

◆ Repeat()

std::string Devel::StringUtils::Repeat ( const std::string &  i_sString,
size_t  i_nTimes 
)
inline

Repeats the input string a specified number of times.

Parameters
i_sStringThe string to repeat.
i_nTimesThe number of times to repeat the string.
Returns
A new string that is the input string repeated the specified number of times.

◆ Replace() [1/2]

std::string Devel::StringUtils::Replace ( const std::string &  i_sSource,
const std::string &  i_sFind,
const std::string &  i_sReplaceWith 
)
inline

Replaces all instances of a specific substring in the source string with another substring.

Parameters
i_sSourceThe source string.
i_sFindThe substring to find.
i_sReplaceWithThe substring to replace with.
Returns
A new string that is the result of the replacement operation.

◆ Replace() [2/2]

std::string& Devel::StringUtils::Replace ( std::string &  i_sSource,
const char  i_bFind,
const char  i_bReplaceWith 
)
inline

Replaces all instances of a specific character in the source string with another character.

Parameters
i_sSourceThe source string.
i_bFindThe character to find.
i_bReplaceWithThe character to replace with.
Returns
A reference to the modified source string.

◆ Set() [1/3]

void Devel::StringUtils::Set ( char *  i_pDestination,
const char *  i_szString,
const size_t  i_nMax 
)
inline

Copies a string to a destination buffer, up to a maximum size.

Parameters
i_pDestinationThe destination buffer.
i_szStringThe string to copy.
i_nMaxThe maximum size.

◆ Set() [2/3]

void Devel::StringUtils::Set ( char *  i_pDestination,
const char *  i_szString,
const size_t  i_nSize,
size_t  i_nMax 
)
inline

Copies a string to a destination buffer, up to a maximum size.

Parameters
i_pDestinationThe destination buffer.
i_szStringThe string to copy.
i_nSizeThe size of the string.
i_nMaxThe maximum size.

◆ Set() [3/3]

void Devel::StringUtils::Set ( char *  i_pDestination,
const std::string &  i_sData,
const size_t  i_nMax 
)
inline

Copies a string to a destination buffer, up to a maximum size.

Parameters
i_pDestinationThe destination buffer.
i_sDataThe string to copy.
i_nMaxThe maximum size.

◆ Size()

size_t Devel::StringUtils::Size ( const char *  i_szString,
const size_t  i_nMaxSize 
)
inline

Returns the size of the string, up to a maximum size.

Parameters
i_szStringThe string to measure.
i_nMaxSizeThe maximum size.
Returns
The size of the string.

◆ Split() [1/2]

std::vector<std::string> Devel::StringUtils::Split ( const char *  i_szString,
size_t  i_nLength,
const char  i_bDelimeter,
const size_t  i_nReserve = -1 
)
inline

Splits the input string into substrings based on the specified delimiter.

Parameters
i_szStringThe string to split.
i_nLengthThe length of the string to split.
i_bDelimeterThe delimiter to use for splitting the string.
i_nReserveThe size to reserve for the resulting vector.
Returns
A vector of strings split based on the specified delimiter.

◆ Split() [2/2]

std::vector<std::string> Devel::StringUtils::Split ( const std::string &  i_sString,
const char  i_bDelimeter,
const size_t  i_nReserve = -1 
)
inline

Splits the input string into substrings based on the specified delimiter.

Parameters
i_sStringThe string to split.
i_bDelimeterThe delimiter to use for splitting the string.
i_nReserveThe size to reserve for the resulting vector.
Returns
A vector of strings split based on the specified delimiter.

◆ ToArray() [1/3]

template<typename T >
void Devel::StringUtils::ToArray ( T &  i_pDestination,
const char *  i_szString 
)

Copies a string to a destination array.

Parameters
i_pDestinationThe destination array.
i_szStringThe string to copy.

◆ ToArray() [2/3]

template<typename T >
void Devel::StringUtils::ToArray ( T &  i_pDestination,
const char *  i_szString,
const size_t  i_nSize 
)

Copies a string to a destination array, up to a maximum size.

Parameters
i_pDestinationThe destination array.
i_szStringThe string to copy.
i_nSizeThe size of the string.

◆ ToArray() [3/3]

template<typename T >
void Devel::StringUtils::ToArray ( T &  i_pDestination,
const std::string &  i_sData 
)

Converts a string to an array.

Template Parameters
TThe type of the destination array.
Parameters
i_pDestinationThe destination array.
i_sDataThe string data to convert.

◆ ToDouble()

double Devel::StringUtils::ToDouble ( const std::string &  i_stBuffer)
inline

Converts a string to a double.

Parameters
i_stBufferThe string to convert.
Returns
The converted double.

◆ ToFloat()

float Devel::StringUtils::ToFloat ( const std::string &  i_stBuffer)
inline

Converts a string to a float.

Parameters
i_stBufferThe string to convert.
Returns
The converted float.

◆ ToHex()

template<typename T , typename std::enable_if< std::is_arithmetic< T >::value >::type * = nullptr>
std::string Devel::StringUtils::ToHex ( i_oValue)
inline

Converts a numeric value to a hex string.

Template Parameters
TA numeric type.
Parameters
i_oValueThe numeric value to convert.
Returns
A string representing the hex value of the input.

◆ ToInt()

int Devel::StringUtils::ToInt ( const std::string &  i_sBuffer)
inline

Converts a string to an integer.

Parameters
i_sBufferThe string to convert.
Returns
The converted integer.

◆ ToInt64()

long long Devel::StringUtils::ToInt64 ( const std::string &  i_sBuffer)
inline

Converts a string to a 64-bit integer.

Parameters
i_sBufferThe string to convert.
Returns
The converted 64-bit integer.

◆ ToLower()

void Devel::StringUtils::ToLower ( std::string &  i_sBuffer)
inline

Converts the string to lower case.

Parameters
i_sBufferThe string to convert.

◆ ToLowerCopy()

std::string Devel::StringUtils::ToLowerCopy ( const std::string &  i_sBuffer)
inline

Creates a copy of the string and converts it to lower case.

Parameters
i_sBufferThe string to copy and convert.
Returns
A copy of the string converted to lower case.

◆ ToUInt()

unsigned int Devel::StringUtils::ToUInt ( const std::string &  i_sBuffer)
inline

Converts a string to an unsigned integer.

Parameters
i_sBufferThe string to convert.
Returns
The converted unsigned integer.

◆ ToUInt64()

unsigned long long Devel::StringUtils::ToUInt64 ( const std::string &  i_sBuffer)
inline

Converts a string to an unsigned 64-bit integer.

Parameters
i_sBufferThe string to convert.
Returns
The converted unsigned 64-bit integer.

◆ ToUpper()

void Devel::StringUtils::ToUpper ( std::string &  i_sBuffer)
inline

Converts the string to upper case.

Parameters
i_sBufferThe string to convert.

◆ ToUpperCopy()

std::string Devel::StringUtils::ToUpperCopy ( const std::string &  i_sBuffer)
inline

Creates a copy of the string and converts it to upper case.

Parameters
i_sBufferThe string to copy and convert.
Returns
A copy of the string converted to upper case.