Hazelnp::StringTools Class Reference

Internal helper class. More...

`#include <StringTools.h>`

## Static Public Member Functions

static bool | Contains (const std::string &str, const char c)
| Will return wether or not a given char is in a string. More...
static std::string | Replace (const std::string &str, const char find, const std::string &subst)
| Will replace a part of a string with another string. More...
static std::string | Replace (const std::string &str, const std::string &find, const std::string &subst)
| Will replace a part of a string with another string. More...
static bool | IsNumeric (const std::string &str, const bool allowDecimalPoint=false)
| Will return true if the given string consists only of digits (including signage) More...
static bool | ParseNumber (const std::string &str, bool &out_isInt, long double &out_number)
| Will convert the number in str to a number. More...
static std::vector< std::string > | SplitString (const std::string &str, const char delimiter)
| Will split a string by a delimiter char. The delimiter will be excluded! More...
static std::vector< std::string > | SplitString (const std::string &str, const std::string &delimiter)
| Will split a string by a delimiter string. The delimiter will be excluded! More...
static std::string | ToLower (const std::string &str)
| Will make a string all lower-case. More...

## Detailed Description

Internal helper class.

Feel free to use it tho.

Definition at line 11 of file StringTools.h.

## Member Function Documentation

## Contains()

bool StringTools::Contains | ( | const std::string & | str,
|  | const char | c
| ) |  |

static

Will return wether or not a given char is in a string.

Definition at line 5 of file StringTools.cpp.

{
for (const char& i : str)
if (i == c)
return true;
return false;
}

## IsNumeric()

bool StringTools::IsNumeric | ( | const std::string & | str,
|  | const bool | allowDecimalPoint = `false`
| ) |  |

static

Will return true if the given string consists only of digits (including signage)

Definition at line 56 of file StringTools.cpp.

{
if (str.length() == 0) return false;
bool alreadyParsedDecimalPoint = false;
std::size_t digitCount = 0;
for (std::size_t i = 0; i < str.length(); i++)
{
if (!(
((str[i] >= '0') && (str[i] <= '9')) ||
((str[i] == '-') && (i == 0)) ||
((str[i] == '.') && (allowDecimalPoint) && (!alreadyParsedDecimalPoint) && (digitCount > 0))
)) return false;
// Here we just have to check for the character. Not for any other conditions.
// Why? Because if these conditions failed, the function would have already returned false.
if (((str[i] >= '0') && (str[i] <= '9'))) digitCount++;
if (str[i] == '.') alreadyParsedDecimalPoint = true;
}
// Even if we did not find any invalid chars, we should still return false, if we found no digits at all.
return digitCount > 0;
}

## ParseNumber()

bool StringTools::ParseNumber | ( | const std::string & | str,
|  | bool & | out_isInt,
|  | long double & | out_number
| ) |  |

static

Will convert the number in str to a number.

Returns wether or not the operation was successful.
Also returns wether the number is an integer, or floating point. If int, cast out_number to int.

Definition at line 82 of file StringTools.cpp.

{
bool isDecimal = false;
if (str.length() == 0) return false;
if (Contains(str, '.')) isDecimal = true;
if (isDecimal)
{
try
{
out_number = std::stold(str);
out_isInt = false;
}
catch (std::invalid_argument&)
{
return false;
}
catch (std::out_of_range&)
{
return false;
}
}
else
{
try
{
out_number = (long double)std::stoll(str);
out_isInt = true;
}
catch (std::invalid_argument&)
{
return false;
}
catch (std::out_of_range&)
{
return false;
}
}
return true;
}

## Replace() [1/2]

std::string StringTools::Replace | ( | const std::string & | str,
|  | const char | find,
|  | const std::string & | subst
| ) |  |

static

Will replace a part of a string with another string.

Definition at line 14 of file StringTools.cpp.

{
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if (str[i] != find) ss << str[i];
else ss << subst;
}
return ss.str();
}

## Replace() [2/2]

std::string StringTools::Replace | ( | const std::string & | str,
|  | const std::string & | find,
|  | const std::string & | subst
| ) |  |

static

Will replace a part of a string with another string.

Definition at line 27 of file StringTools.cpp.

{
if (find.length() == 0) return str;
std::stringstream ss;
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(find, posFound);
if (posFound != std::string::npos)
{
ss << str.substr(lastFound, posFound - lastFound) << subst;
posFound += find.length();
}
else
{
ss << str.substr(lastFound, (str.length()) - lastFound);
}
}
return ss.str();
}

## SplitString() [1/2]

std::vector< std::string > StringTools::SplitString | ( | const std::string & | str,
|  | const char | delimiter
| ) |  |

static

Will split a string by a delimiter char. The delimiter will be excluded!

Definition at line 125 of file StringTools.cpp.

{
if (str.length() == 0) return std::vector<std::string>();
return SplitString(str, delimiter);
}

## SplitString() [2/2]

std::vector< std::string > StringTools::SplitString | ( | const std::string & | str,
|  | const std::string & | delimiter
| ) |  |

static

Will split a string by a delimiter string. The delimiter will be excluded!

Definition at line 132 of file StringTools.cpp.

{
if (str.length() == 0) return std::vector<std::string>();
std::vector<std::string> parts;
if (delimiter.length() == 0) // If the delimiter is "" (empty), just split between every single char. Not useful, but logical
{
for (std::size_t i = 0; i < str.length(); i++)
{
parts.push_back(std::string({ str[i] }));
}
return parts;
}
std::size_t posFound = 0;
std::size_t lastFound = 0;
while (posFound != std::string::npos)
{
lastFound = posFound;
posFound = str.find(delimiter, posFound);
std::string found;
if (posFound != std::string::npos)
{
found = str.substr(lastFound, posFound - lastFound);
posFound += delimiter.length();
}
else
{
found = str.substr(lastFound, str.length() - lastFound);
}
parts.push_back(found);
}
return parts;
}

## ToLower()

std::string StringTools::ToLower | ( | const std::string & | str | ) |

static

Will make a string all lower-case.

Definition at line 173 of file StringTools.cpp.

{
std::stringstream ss;
for (std::size_t i = 0; i < str.length(); i++)
{
if ((str[i] >= 'A') && (str[i] <= 'Z')) ss << (char)(((int)str[i]) + 32);
else if (str[i] == -60) ss << (char)-28; // AE => ae
else if (str[i] == -42) ss << (char)-10; // OE => oe
else if (str[i] == -36) ss << (char)-4; // UE => ue
else ss << str[i];
}
return ss.str();
}

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

- Hazelnupp/StringTools.h
- Hazelnupp/StringTools.cpp

Hazelnp::StringTools::Contains
static bool Contains(const std::string &str, const char c)
Will return wether or not a given char is in a string.
Definition: StringTools.cpp:5
Hazelnp::StringTools::SplitString
static std::vector< std::string > SplitString(const std::string &str, const char delimiter)
Will split a string by a delimiter char. The delimiter will be excluded!
Definition: StringTools.cpp:125