CppCommon  1.0.4.1
C++ Common Library
string_utils.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_STRING_STRING_UTILS_H
10 #define CPPCOMMON_STRING_STRING_UTILS_H
11 
12 #include <algorithm>
13 #include <cctype>
14 #include <cstdint>
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 namespace CppCommon {
20 
22 
29 {
30 public:
31  StringUtils() = delete;
32  StringUtils(const StringUtils&) = delete;
33  StringUtils(StringUtils&&) = delete;
34  ~StringUtils() = delete;
35 
36  StringUtils& operator=(const StringUtils&) = delete;
38 
40 
44  static bool IsBlank(char ch);
46 
50  static bool IsBlank(const char* str);
52 
56  static bool IsBlank(std::string_view str);
57 
59 
74  static bool IsPatternMatch(const std::string& patterns, const std::string& str);
75 
77 
80  static char ToLower(char ch);
82 
85  static char ToUpper(char ch);
86 
88 
92  static std::string ToLower(std::string_view str);
94 
98  static std::string ToUpper(std::string_view str);
99 
101 
105  static std::string& Lower(std::string& str);
107 
111  static std::string& Upper(std::string& str);
112 
114 
118  static std::string ToLTrim(std::string_view str);
120 
124  static std::string ToRTrim(std::string_view str);
126 
130  static std::string ToTrim(std::string_view str);
131 
133 
137  static std::string& LTrim(std::string& str);
139 
143  static std::string& RTrim(std::string& str);
145 
149  static std::string& Trim(std::string& str);
150 
152 
156  static std::string RemoveBlank(std::string_view str);
158 
162  static std::string& RemoveBlank(std::string& str);
163 
165 
170  static bool Compare(std::string_view str1, std::string_view str2);
172 
177  static bool CompareNoCase(std::string_view str1, std::string_view str2);
178 
180 
185  static bool Contains(std::string_view str, const char ch);
187 
192  static bool Contains(std::string_view str, const char* substr);
194 
199  static bool Contains(std::string_view str, std::string_view substr);
200 
202 
207  static size_t CountAll(std::string_view str, std::string_view substr);
208 
210 
216  static bool ReplaceFirst(std::string& str, std::string_view substr, std::string_view with);
218 
224  static bool ReplaceLast(std::string& str, std::string_view substr, std::string_view with);
226 
232  static bool ReplaceAll(std::string& str, std::string_view substr, std::string_view with);
233 
235 
240  static bool StartsWith(std::string_view str, std::string_view prefix);
242 
247  static bool EndsWith(std::string_view str, std::string_view suffix);
248 
250 
256  static std::vector<std::string> Split(std::string_view str, char delimiter, bool skip_empty = false);
258 
264  static std::vector<std::string> Split(std::string_view str, std::string_view delimiter, bool skip_empty = false);
266 
272  static std::vector<std::string> SplitByAny(std::string_view str, std::string_view delimiters, bool skip_empty = false);
273 
275 
281  static std::string Join(const std::vector<std::string>& tokens, bool skip_empty = false, bool skip_blank = false);
283 
290  static std::string Join(const std::vector<std::string>& tokens, char delimiter, bool skip_empty = false, bool skip_blank = false);
292 
299  static std::string Join(const std::vector<std::string>& tokens, const char* delimiter, bool skip_empty = false, bool skip_blank = false);
301 
308  static std::string Join(const std::vector<std::string>& tokens, std::string_view delimiter, bool skip_empty = false, bool skip_blank = false);
309 
311 
315  template <typename T>
316  static std::string ToString(const T& value);
318 
322  template <typename T>
323  static T FromString(std::string_view str);
324 
325 private:
326  static bool IsBlankInternal(char ch);
327  static char ToLowerInternal(char ch);
328  static char ToUpperInternal(char ch);
329 };
330 
333 } // namespace CppCommon
334 
335 #include "string_utils.inl"
336 
337 #endif // CPPCOMMON_STRING_STRING_UTILS_H
String utilities.
Definition: string_utils.h:29
static bool Compare(std::string_view str1, std::string_view str2)
Compare two strings case sensitive version.
static std::vector< std::string > SplitByAny(std::string_view str, std::string_view delimiters, bool skip_empty=false)
Split the string into tokens by the any character in the given delimiter string.
static size_t CountAll(std::string_view str, std::string_view substr)
Count all occurrences of substring.
static std::string & Trim(std::string &str)
Trims space characters from the both sides of the given string.
static std::vector< std::string > Split(std::string_view str, char delimiter, bool skip_empty=false)
Split the string into tokens by the given delimiter character.
StringUtils & operator=(const StringUtils &)=delete
static std::string & Lower(std::string &str)
Convert the given string to lower case.
static std::string ToRTrim(std::string_view str)
Trims space characters from the end of the given constant string.
static bool StartsWith(std::string_view str, std::string_view prefix)
Checks the given string for specific prefix.
static std::string & LTrim(std::string &str)
Trims space characters from the start of the given string.
static std::string ToString(const T &value)
Converts arbitrary datatypes into string using std::ostringstream.
static T FromString(std::string_view str)
Converts strings to arbitrary datatypes using std::istringstream.
static bool ReplaceAll(std::string &str, std::string_view substr, std::string_view with)
Replace all occurrences of substring with another substring.
static std::string ToLTrim(std::string_view str)
Trims space characters from the start of the given constant string.
static std::string Join(const std::vector< std::string > &tokens, bool skip_empty=false, bool skip_blank=false)
Join tokens into the string.
static char ToLower(char ch)
Convert the given character to lower case.
static bool ReplaceFirst(std::string &str, std::string_view substr, std::string_view with)
Replace the first occurrence of substring with another substring.
static std::string & RTrim(std::string &str)
Trims space characters from the end of the given string.
StringUtils(const StringUtils &)=delete
static bool CompareNoCase(std::string_view str1, std::string_view str2)
Compare two strings case insensitive version.
static std::string ToTrim(std::string_view str)
Trims space characters from the both sides of the given constant string.
static char ToUpper(char ch)
Convert the given character to UPPER case.
static bool IsBlank(char ch)
Is the given character blank (empty or contains only space characters)?
static bool ReplaceLast(std::string &str, std::string_view substr, std::string_view with)
Replace the last occurrence of substring with another substring.
StringUtils & operator=(StringUtils &&)=delete
static bool Contains(std::string_view str, const char ch)
Is the given string contains the given character?
static std::string & Upper(std::string &str)
Convert the given string to UPPER case.
static bool EndsWith(std::string_view str, std::string_view suffix)
Checks the given string for specific suffix.
static bool IsPatternMatch(const std::string &patterns, const std::string &str)
Is the given string match to the given patterns?
static std::string RemoveBlank(std::string_view str)
Remove blank characters from the given string.
StringUtils(StringUtils &&)=delete
C++ Common project definitions.
Definition: token_bucket.h:15
String utilities inline implementation.