namespace
#include <werkzeugkiste/strings/strings.h>
strings Common string manipulation & check utilities.
Contents
- Reference
Functions
- auto EndsWith(std::string_view s, std::string_view suffix) -> bool constexpr noexcept
- Returns true if the string ends with the given suffix.
- auto EndsWith(std::string_view s, char end) -> bool constexpr noexcept
- Returns true if the string ends with the given character.
- auto StartsWith(std::string_view s, std::string_view prefix) -> bool constexpr noexcept
- Returns true if the given string starts with the prefix.
- auto StartsWith(std::string_view s, char first) -> bool constexpr noexcept
- Returns true if the string starts with the given character.
- auto ToLower(std::string& s) -> WERKZEUGKISTE_STRINGS_EXPORT void
- Converts the string to lower case (in-place).
- auto Lower(std::string_view s) -> std::string
- Returns a copy, converted to lower case.
- auto ToUpper(std::string& s) -> WERKZEUGKISTE_STRINGS_EXPORT void
- Converts the string to upper case (in-place).
- auto Upper(const std::string& s) -> std::string
- Returns a copy, converted to upper case.
- auto LengthDifference(std::string_view str1, std::string_view str2) -> std::size_t
- Returns the absolute length difference of the two strings.
- auto Trim(std::string_view s) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a copy with leading & trailing white space removed.
- auto LTrim(std::string_view totrim) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a copy with leading white space removed.
- auto RTrim(std::string_view totrim) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a copy with trailing white space removed.
- auto IsNumeric(const std::string& s) -> WERKZEUGKISTE_STRINGS_EXPORT bool
- Returns true if the string can be safely cast into either an
int64_t
or adouble
type. - auto IsInteger(std::string_view str) -> WERKZEUGKISTE_STRINGS_EXPORT bool
- Returns true if the input string is a valid integer, i.e. "[+-]?[0-9]+".
- auto Split(std::string_view s, char delim) -> WERKZEUGKISTE_STRINGS_EXPORT std::vector<std::string>
- Tokenizes the string by the given delimiter.
- auto Tokenize(std::string_view s, std::string_view delim) -> WERKZEUGKISTE_STRINGS_EXPORT std::vector<std::string_view>
- Tokenizes the string by the given delimiter.
-
template<typename Container, typename Tp = std::decay_t<decltype(*begin(std::declval<Container>()))>>auto Concatenate(const Container& container, std::string_view delimiter = "") -> std::string
- Concatenates all strings in the given container.
- auto Replace(std::string_view haystack, std::string_view needle, std::string_view replacement) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Replaces all occurrences of the given search string
needle
within thehaystack
. - auto Replace(std::string_view haystack, char needle, char replacement) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Replaces all occurrences of the given character.
- auto ClipUrl(const std::string& url) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Clips the given URL string to include only the protocol and domain, i.e. server paths & parameters will be excluded.
- auto GetUrlProtocol(const std::string& url, std::string& protocol, std::string& remainder) -> WERKZEUGKISTE_STRINGS_EXPORT bool
- Splits a URL into protocol and remainder.
- auto ObscureUrlAuthentication(const std::string& url) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns the URL after replacing any plaintext authentication data by the text
<auth>
. - auto Remove(std::string_view s, std::initializer_list<char> chars) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a copy where all given characters have been removed.
- auto Remove(std::string_view s, char c) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a copy where the given character has been removed.
- auto Slug(std::string_view s, bool strip_dashes = false) -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a slug representation of the string.
- auto Shorten(std::string_view s, std::size_t desired_length, int ellipsis_position = -1, std::string_view ellipsis = "...") -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns a string with length <=
desired_length
, where the customizableellipsis
has been inserted to indicate that the input string has been clipped. - auto Indent(std::string_view s, std::size_t n, char character = ' ') -> WERKZEUGKISTE_STRINGS_EXPORT std::string
- Returns the string indented by n-times the given character.
- auto LevenshteinDistance(std::string_view str1, std::string_view str2) -> WERKZEUGKISTE_STRINGS_EXPORT std::size_t
- Returns the the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one string into the other.
Function documentation
std::size_t werkzeugkiste:: strings:: LengthDifference(std::string_view str1,
std::string_view str2)
Returns the absolute length difference of the two strings.
Parameters | |
---|---|
str1 | First string. |
str2 | Second string. |
Returns | :math:|len(str1) - len(str2)| |
WERKZEUGKISTE_STRINGS_EXPORT std::vector<std::string> werkzeugkiste:: strings:: Split(std::string_view s,
char delim)
Tokenizes the string by the given delimiter.
Note that an empty trailing token will be skipped. For example: Split("a-b-c", '-') returns the same 3 tokens (namely, "a", "b" and "c") as Split("a-b-c-", '-'). For "a-b-c--", however, "a", "b", "c" and "" would be returned.
WERKZEUGKISTE_STRINGS_EXPORT std::vector<std::string_view> werkzeugkiste:: strings:: Tokenize(std::string_view s,
std::string_view delim)
Tokenizes the string by the given delimiter.
Note that empty tokens will be skipped. For example: Tokenize("a-b-c", "-") returns the same 3 tokens (namely, "a", "b" and "c") as Tokenize("a-b-c-", "-") and also "-a-b-c--".
WERKZEUGKISTE_STRINGS_EXPORT bool werkzeugkiste:: strings:: GetUrlProtocol(const std::string& url,
std::string& protocol,
std::string& remainder)
Splits a URL into protocol and remainder.
Sets protocol
to the URL's protocol, e.g. https:/
, rtp://
, etc. Returns true if the url
string contained a protocol part.
WERKZEUGKISTE_STRINGS_EXPORT std::string werkzeugkiste:: strings:: Slug(std::string_view s,
bool strip_dashes = false)
Returns a slug representation of the string.
The input will be converted to lower case & trimmed. The number sign/hash will be replaced by "nr". Any other non-alphanumeric symbols will be replaced by dashes. If strip_dashes
is true, the remaining dashes will then also be stripped: e.g. img_dir
would become imgdir
.
WERKZEUGKISTE_STRINGS_EXPORT std::string werkzeugkiste:: strings:: Shorten(std::string_view s,
std::size_t desired_length,
int ellipsis_position = -1,
std::string_view ellipsis = "...")
Returns a string with length <= desired_length
, where the customizable ellipsis
has been inserted to indicate that the input string has been clipped.
Argument ellipsis_position specifies where the ellipsis will be placed:
< 0
: Left0
: Centered> 0
: Right
WERKZEUGKISTE_STRINGS_EXPORT std::size_t werkzeugkiste:: strings:: LevenshteinDistance(std::string_view str1,
std::string_view str2)
Returns the the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one string into the other.
Parameters | |
---|---|
str1 | First string. |
str2 | Second string. |
Returns | The edit distance >= 0. |