String Manipulators
The following are some common functions used to manipulate and interact with strings (or, more often, string_views, due to the performance gain).
String Split
-
std::vector<std::string_view> split(std::string_view string, std::string_view delimiter)
Splits a string into a vector, with each element indicated by the delimiter string.
Warning
The output is a vector of string_views - references to the original string. This has the limitation that the output is only meaningful so long as the original string survives. Copies out of the string_view do persist
- Parameters:
string – A (view) of a string to be split
delimiter – The string which indicates a ‘break’. Delimiters do not appear in the split output
- Returns:
A vector of windows into the original string, indicating which elements have been grouped together.
String Trim
-
inline std::string_view trim(std::string_view sv)
Removes leading or trailing whitespace from a string_view
- Parameters:
sv – The original string_view
- Returns:
A modified string_view with no leading or trailing whitespace
-
inline std::string_view trim(std::string_view sv, const std::string &commentIndicator)
Removes leading or trailing whitespace from a string_view and trims any ‘comments’ from the string.
Comments are signified by the ‘commentIndicator’, and all text after the indicator is removed. With indicator ‘#’, the string “hello, my name is #Put your name here” would be trimmed to “hello my name is”;
- Parameters:
sv – The original string_view
commentIndicator – The string after which all text is to be removed.
- Returns:
A modified string_view with no leading or trailing whitespace
Insensitive Equals
-
inline bool insensitiveEquals(const std::string_view a, const std::string_view b)
Performs a case-insensitive equality check on two strings
- Returns:
True if a and b are (aside from cases) equal strings