General Utility Library for C++14  2.12
Functions
gul14/trim.h

Detailed Description

Trimming whitespace from strings.

Functions

GUL_EXPORT std::string gul14::trim (string_view str, string_view ws_chars=default_whitespace_characters)
 Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a new std::string. More...
 
GUL_EXPORT string_view gul14::trim_sv (string_view str, string_view ws_chars=default_whitespace_characters)
 Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a view into the original string. More...
 
GUL_EXPORT std::string gul14::trim_left (string_view str, string_view ws_chars=default_whitespace_characters)
 Trim leading whitespace (or a custom set of characters) from a string, returning a new std::string. More...
 
GUL_EXPORT string_view gul14::trim_left_sv (string_view str, string_view ws_chars=default_whitespace_characters)
 Trim leading whitespace (or a custom set of characters) from a string, returning a view into the original string. More...
 
GUL_EXPORT std::string gul14::trim_right (string_view str, string_view ws_chars=default_whitespace_characters)
 Trim trailing whitespace (or a custom set of characters) from a string, returning a new std::string. More...
 
GUL_EXPORT string_view gul14::trim_right_sv (string_view str, string_view ws_chars=default_whitespace_characters)
 Trim trailing whitespace (or a custom set of characters) from a string, returning a view into the original string. More...
 

Function Documentation

◆ trim()

std::string gul14::trim ( string_view  str,
string_view  ws_chars = default_whitespace_characters 
)

Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a new std::string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim("\n \b trim(), default whitespace\t \r") << "]\n";
// prints "[trim(), default whitespace]"
cout << "[" << trim(".:.:.:trim(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[trim(), custom whitespace]"
GUL_EXPORT std::string trim(string_view str, string_view ws_chars=default_whitespace_characters)
Trim leading and trailing whitespace (or a custom set of characters) from a string,...
Definition: trim.cc:29
Parameters
strThe string that should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed copy of the input string.
See also
trim_sv() returns a string_view instead of a copied string,
trim_left() and trim_right() trim only one side of the string,
trim_left_sv() and trim_right_sv() trim only one side and return a string_view.
Examples
trim.cc.

References gul14::trim_sv().

◆ trim_left()

std::string gul14::trim_left ( string_view  str,
string_view  ws_chars = default_whitespace_characters 
)

Trim leading whitespace (or a custom set of characters) from a string, returning a new std::string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left("\n \b trim_left(), default whitespace ") << "]\n";
// prints "[trim_left(), default whitespace ]"
cout << "[" << trim_right(".:.:.:trim_right(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right, custom whitespace]"
GUL_EXPORT std::string trim_left(string_view str, string_view ws_chars=default_whitespace_characters)
Trim leading whitespace (or a custom set of characters) from a string, returning a new std::string.
Definition: trim.cc:46
GUL_EXPORT std::string trim_right(string_view str, string_view ws_chars=default_whitespace_characters)
Trim trailing whitespace (or a custom set of characters) from a string, returning a new std::string.
Definition: trim.cc:61
Parameters
strThe string from which leading characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed copy of the input string.
See also
trim_left_sv() returns a string_view instead of a copied string,
trim_right() and trim_right_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

References gul14::trim_left_sv().

◆ trim_left_sv()

string_view gul14::trim_left_sv ( string_view  str,
string_view  ws_chars = default_whitespace_characters 
)

Trim leading whitespace (or a custom set of characters) from a string, returning a view into the original string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left_sv("\n \b trim_left_sv(), default whitespace ") << "]\n";
// prints "[trim_left_sv(), default whitespace ]"
cout << "[" << trim_right_sv(".:.:.:trim_right_sv(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right_sv, custom whitespace]"
GUL_EXPORT string_view trim_right_sv(string_view str, string_view ws_chars=default_whitespace_characters)
Trim trailing whitespace (or a custom set of characters) from a string, returning a view into the ori...
Definition: trim.cc:66
GUL_EXPORT string_view trim_left_sv(string_view str, string_view ws_chars=default_whitespace_characters)
Trim leading whitespace (or a custom set of characters) from a string, returning a view into the orig...
Definition: trim.cc:51
Parameters
strThe string from which leading characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed string_view that points into the input string.
See also
trim_left() returns a copied string instead of a string_view,
trim_right() and trim_right_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

Referenced by gul14::trim_left().

◆ trim_right()

std::string gul14::trim_right ( string_view  str,
string_view  ws_chars = default_whitespace_characters 
)

Trim trailing whitespace (or a custom set of characters) from a string, returning a new std::string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left("\n \b trim_left(), default whitespace ") << "]\n";
// prints "[trim_left(), default whitespace ]"
cout << "[" << trim_right(".:.:.:trim_right(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right, custom whitespace]"
Parameters
strThe string from which trailing characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed copy of the input string.
See also
trim_right_sv() returns a string_view instead of a copied string,
trim_left() and trim_left_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

References gul14::trim_right_sv().

◆ trim_right_sv()

string_view gul14::trim_right_sv ( string_view  str,
string_view  ws_chars = default_whitespace_characters 
)

Trim trailing whitespace (or a custom set of characters) from a string, returning a view into the original string.

Which characters are removed can be customized via the ws_chars parameter.

cout << "[" << trim_left_sv("\n \b trim_left_sv(), default whitespace ") << "]\n";
// prints "[trim_left_sv(), default whitespace ]"
cout << "[" << trim_right_sv(".:.:.:trim_right_sv(), custom whitespace.:.:.:.", ".:") << "]\n";
// prints "[.:.:.:trim_right_sv, custom whitespace]"
Parameters
strThe string from which trailing characters should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed string_view that points into the input string.
See also
trim_right() returns a copied string instead of a string_view,
trim_left() and trim_left_sv() trim the other side of the string,
trim() and trim_sv() trim both sides of the string.
Examples
trim.cc.

Referenced by gul14::trim_right().

◆ trim_sv()

string_view gul14::trim_sv ( string_view  str,
string_view  ws_chars = default_whitespace_characters 
)

Trim leading and trailing whitespace (or a custom set of characters) from a string, returning a view into the original string.

Which characters are removed can be customized via the ws_chars parameter.

std::string str = " string_view ";
auto sv = trim_sv(str); // sv is a string_view that points to the original string
cout << "[" << sv << "]\n";
// prints "[string_view]"
str[5] = 'o'; // modify the original string
cout << "[" << sv << "]\n";
// prints "[strong_view]"
GUL_EXPORT string_view trim_sv(string_view str, string_view ws_chars=default_whitespace_characters)
Trim leading and trailing whitespace (or a custom set of characters) from a string,...
Definition: trim.cc:34
Parameters
strThe string that should be trimmed.
ws_charsA string containing all the characters that should be treated as whitespace (i.e. that are trimmed). If this is empty, no characters are trimmed.
Returns
a trimmed string_view that points into the input string.
See also
trim() returns a copied string instead of a string_view,
trim_left_sv() and trim_right_sv() trim only one side of the string,
trim_left() and trim_right() trim only one side and return a copied string.
Examples
trim.cc.

Referenced by gul14::trim().