这里只做展示作用,可能更新不及时,获取源码请移步gitee个人仓库:string_helper
#ifndef _STRING_HELPER_
#define _STRING_HELPER_
#include <string>
#include <cctype>
#include <algorithm>
#include <set>
#include <functional>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include <cmath>
//宏定义:参数流向
#define IN
#define OUT
#define INOUT
class StringHelper {
public:
inline static void Capitalize(INOUT std::string& str);
inline static void Title(INOUT std::string& str);
inline static void Center(INOUT std::string& str, IN size_t width, IN const char fillchar = ' ');
inline static void Ljust(INOUT std::string& str, IN size_t width, IN const char fillchar = ' ');
inline static void Rjust(INOUT std::string& str, IN size_t width, IN const char fillchar = ' ');
inline static void Zfill(INOUT std::string& str, IN size_t width);
inline static void Lstrip(INOUT std::string& str, IN const std::string& chars = " ");
inline static void Rstrip(INOUT std::string& str, IN const std::string& chars = " ");
inline static void Strip(INOUT std::string& str, IN const std::string& chars = " ");
inline static void Lower(INOUT std::string& str);
inline static void Upper(INOUT std::string& str);
inline static void Swapcase(INOUT std::string& str);
inline static void CamelCase2Underline(INOUT std::string& str);
inline static void Underline2CamelCase(INOUT std::string& str, IN bool toLowerCamelCase = false);
inline static std::vector<std::string> Split(IN const std::string& str, IN const std::string& delims = " ");
inline static std::vector<std::string> SplitCamelCase(IN const std::string& str);
inline static std::vector<std::string> SplitUnderline(IN const std::string& str);
inline static std::vector<std::string> SplitLines(IN const std::string& str, IN bool keepends = false);
inline static char Max(IN const std::string& str);
inline static char Min(IN const std::string& str);
inline static size_t Count(IN const std::string& str, IN const std::string& sub,
IN size_t start = 0, IN size_t end = 0);
inline static bool Isalnum(IN const std::string& str);
inline static bool Isalpha(IN const std::string& str);
inline static bool Isdigit(IN const std::string& str);
inline static bool IsdigitWithDelim(IN const std::string& str, IN const std::string& delims = " ");
inline static bool Isxdigit(IN const std::string& str);
inline static bool IsxdigitWithDelim(IN const std::string& str, IN const std::string& delims = " ");
inline static bool Islower(IN const std::string& str);
inline static bool Isupper(IN const std::string& str);
inline static bool Isspace(IN const std::string& str);
inline static bool IsTitle(IN const std::string& str);
inline static bool StartsWith(IN const std::string& str, IN const std::string& sub,
IN size_t start = 0, IN size_t end = 0);
inline static bool EndsWith(IN const std::string& str, IN const std::string& sub,
IN size_t start = 0, IN size_t end = 0);
inline static std::string Join(IN const std::vector<std::string>& token, IN const std::string& linkChars = " ");
inline static std::vector<std::string> ToVector(IN const std::string& str);
template<class T>
inline static T ToNum(IN const std::string& str);
template<class T>
inline static std::vector<T> ExtractNum(IN const std::string& str, IN const std::string& delims = " ");
inline static std::string ReadLine();
inline static std::string ReadLineEndByEnter(IN size_t size = 1);
inline static std::vector<std::string> ReadLines(IN size_t size);
};
/*
* 功能:将字符串的第一个字母变成大写,其它字母变小写。
*/
inline void StringHelper::Capitalize(INOUT std::string& str) {
if (str.size() >= 1) {
str[0] = toupper(str[0]);
std::transform(str.begin() + 1, str.end(), str.begin() + 1, tolower);
}
}
/*
* 功能:返回一个指定的宽度width居中的字符串,fillchar为填充的字符,默认为空格。
* 注意:若`(width - str.length) % 2 != 0`,则前边的填充字符数比后边的多1。
*/
inline void StringHelper::Center(INOUT std::string& str, IN size_t width, IN const char fillchar) {
if (width > str.size()) {
size_t padLen = width - str.size();
std::string newStr(width, fillchar);
std::move(str.begin(), str.end(), newStr.begin() + ceil(padLen / 2.0));
str = std::move(newStr);
}
}
/*
* 功能:返回一个原字符串左对齐,并使用某种字符(默认空格)填充至指定长度的新字符串。
* 如果指定的长度小于原字符串的长度则返回原字符串。
*/
inline void StringHelper::Ljust(std::string& str, size_t width, const char fillchar) {
if (width > str.size()) {
size_t oldSize = str.size();
str.resize(width);
std::fill(str.begin() + oldSize, str.end(), fillchar);
}
}
/*
* 功能:返回一个原字符串右对齐,并使用某种字符(默认空格)填充至指定长度的新字符串。
* 如果指定的长度小于原字符串的长度则返回原字符串。
*/
inline void StringHelper::Rjust(std::string& str, size_t width, const char fillchar) {
if (width > str.size()) {
std::string newStr(width, fillchar);
std::move(str.begin(), str.end(), newStr.begin() + width - str.size());
str = std::move(newStr);
}
}
/*
* 功能:返回指定长度的字符串,原字符串右对齐,前面填充0。
*/
inline void StringHelper::Zfill(INOUT std::string& str, IN size_t width) {
StringHelper::Rjust(str, width, '0');
}
/*
* 功能:用于统计字符串里某个字符出现的次数
* 参数:sub:搜索的子字符串
* start:字符串开始搜索的位置。默认0为第一个字符。
* end:字符串中结束搜索的位置。默认0为最后一个字符。
* 注意:若end大于字符串长度,则自动设置为最后一个字符。
*/
inline size_t StringHelper::Count(IN const std::string& str, IN const std::string& sub,
IN size_t start, IN size_t end) {
if (start > str.size() - 1) {
return 0;
}
if (end == 0 ||
end > str.size() - 1) {
end = str.size() - 1;
}
if (end - start + 1 < str.size()) {
std::string newStr = str.substr(start, end + 1);
size_t count = 0;
size_t pos = newStr.find(sub);
while (pos != std::string::npos) {
count++;
pos = newStr.find(sub, std::min(pos + sub.size(), newStr.size() - 1));
}
return count;
} else {
size_t count = 0;
size_t pos = str.find(sub);
while (pos != std::string::npos) {
count++;
pos = str.find(sub, std::min(pos + sub.size(), str.size() - 1));
}
return count;
}
}
/*
* 功能:用于判断字符串是否以指定前缀开始,如果以指定前缀开始则返回true,否则返回false。
* 可选参数"start"与"end"为检索字符串的开始与结束位置。
* 参数:sub:搜索的子字符串
* start:字符串开始搜索的位置。默认0为第一个字符。
* end:字符串中结束搜索的位置。默认0为最后一个字符。
* 注意:若end大于字符串长度,则自动设置为最后一个字符。
*/
inline bool StringHelper::StartsWith(IN const std::string& str, IN const std::string& sub,
IN size_t start, IN size_t end) {
if (start > str.size() - 1) {
return false;
}
if (end == 0 ||
end > str.size() - 1) {
end = str.size() - 1;
}
if (end - start + 1 < sub.size()) {
return false;
} else {
return std::equal(str.begin() + start, str.begin() + +start + sub.size(), sub.begin());
}
}
/*
* 功能:用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾则返回true,否则返回false。
* 可选参数"start"与"end"为检索字符串的开始与结束位置。
* 参数:sub:搜索的子字符串
* start:字符串开始搜索的位置。默认0为第一个字符。
* end:字符串中结束搜索的位置。默认0为最后一个字符。
* 注意:若end大于字符串长度,则自动设置为最后一个字符。
*/
inline bool StringHelper::EndsWith(IN const std::string& str, IN const std::string& sub,
IN size_t start, IN size_t end) {
if (start > str.size() - 1) {
return false;
}
if (end == 0 ||
end > str.size() - 1) {
end = str.size() - 1;
}
if (end - start + 1 < sub.size()) {
return false;
} else {
return std::equal(str.rbegin() + (str.size() - end - 1),
str.rbegin() + (str.size() - end - 1 + sub.size()),
sub.rbegin());
}
}
/*
* 功能:将序列中的字符串以指定的字符连接生成一个新的字符串。
*/
inline std::string StringHelper::Join(IN const std::vector<std::string>& token, IN const std::string& linkChars) {
std::string linkdStr;
for (size_t i = 0; i < token.size() - 1; i++) {
linkdStr.append(token[i] + linkChars);
}
linkdStr.append(*(token.end() - 1));
return linkdStr;
}
/*
* 功能:字符串逐字符切割转vector。
*/
inline std::vector<std::string> StringHelper::ToVector(IN const std::string& str) {
std::vector<std::string> token;
token.reserve(str.size());
std::for_each(str.begin(), str.end(), [&](const char& e) { token.push_back(std::string(1, e)); });
return token;
}
/*
* 功能:返回“标题化”的字符串,就是说所有单词的首个字母转化为大写,其余字母均为小写(见IsTitle())。
*/
inline void StringHelper::Title(IN std::string& str) {
if (!str.empty()) {
// may add some char else in the future
std::set<char> punctuation{',', '.', '?'};
str[0] = toupper(str[0]);
for (size_t i = 1; i < str.size(); i++) {
char prevChar = str[i - 1];
if (isspace(prevChar) || isdigit(prevChar) || punctuation.find(prevChar) != punctuation.end()) {
str[i] = toupper(str[i]);
} else {
str[i] = tolower(str[i]);
}
}
}
}
/*
* 功能:检测字符串是否只由字母和数字(十进制)组成。
*/
inline bool StringHelper::Isalnum(IN const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), isalnum);
} else {
return false;
}
}
/*
* 功能:检测字符串是否只由字母组成。
*/
inline bool StringHelper::Isalpha(const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), isalpha);
} else {
return false;
}
}
/*
* 功能:检测字符串是否只由数字(十进制)组成。
*/
inline bool StringHelper::Isdigit(const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), isdigit);
} else {
return false;
}
}
/*
* 功能:检测字符串是不是以某种分隔符分隔的数字(十进制)字符串。
* 支持单个分隔符,例如:delims = ","
* 也支持多个分隔符,例如:delims = ",-"
*/
inline bool StringHelper::IsdigitWithDelim(IN const std::string& str, IN const std::string& delims) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), [&](const char& e) {
if (isdigit(e) ||
delims.find(e) != std::string::npos) {
return true;
} else {
return false;
}
});
} else {
return false;
}
}
/*
* 功能:检测字符串是否只由数字(16进制)组成。
*/
inline bool StringHelper::Isxdigit(const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), isxdigit);
} else {
return false;
}
}
/*
* 功能:检测字符串是不是以某种分隔符分隔的数字(十六进制)字符串。
* 支持单个分隔符,例如:delims = ","
* 也支持多个分隔符,例如:delims = ",-"
*/
inline bool StringHelper::IsxdigitWithDelim(IN const std::string& str, IN const std::string& delims) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), [&](const char& e) {
if (isxdigit(e) ||
delims.find(e) != std::string::npos) {
return true;
} else {
return false;
}
});
} else {
return false;
}
}
/*
* 功能:检测字符串是否只由小写字母组成。
*/
inline bool StringHelper::Islower(const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), islower);
} else {
return false;
}
}
/*
* 功能:检测字符串是否只由大写字母组成。
*/
inline bool StringHelper::Isupper(const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), isupper);
} else {
return false;
}
}
/*
* 功能:检测字符串是否只包含空白。
*/
inline bool StringHelper::Isspace(const std::string& str) {
if (!str.empty()) {
return std::all_of(str.begin(), str.end(), isspace);
} else {
return false;
}
}
/*
* 功能:如果字符串是标题化的(见Title())则返回true,否则返回false。
*/
inline bool StringHelper::IsTitle(const std::string& str) {
if (str.empty() || islower(str[0])) {
return false;
} else {
// may add some char else in the future
std::set<char> punctuation{',', '.', '?'};
for (size_t i = 1; i < str.size(); i++) {
char prevChar = str[i - 1];
if (isspace(prevChar) || isdigit(prevChar) || punctuation.find(prevChar) != punctuation.end()) {
if (islower(str[i])) {
return false;
}
} else {
if (isupper(str[i])) {
return false;
}
}
}
return true;
}
}
/*
* 功能:转换字符串中所有大写字符为小写。
*/
inline void StringHelper::Lower(IN std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), tolower);
}
/*
* 功能:转换字符串中所有小写字符为大写。
*/
inline void StringHelper::Upper(IN std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), toupper);
}
/*
* 功能:对字符串的大小写字母进行转换。
*/
inline void StringHelper::Swapcase(IN std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), [](char& e) {
if (islower(e)) {
e = toupper(e);
} else if (isupper(e)) {
e = tolower(e);
}
return e;
});
}
/*
* 功能:驼峰(大驼峰和小驼峰均可)转下划线。
* 例如:“ZhangSan"和"zhangSan"均可转成"zhang_san"。
* 注意:需要用户保证输入是正确的驼峰,否则转换结果未知。
*/
inline void StringHelper::CamelCase2Underline(INOUT std::string& str) {
if (!str.empty()) {
std::string newStr;
for (size_t i = 0; i < str.size(); i++) {
if (i != str.size() - 1 && isupper(str[i + 1])) {
newStr += tolower(str[i]);
newStr += '_';
} else