#include <cctype>
#include <vector>
#include <optional>
#include <string>
#include <stdexcept>
#include <fstream>
class Utils {
public:
static bool endswith(const std::string &a, const std::string &b) {
if (a.rfind(b) == (a.length() - b.length())) {
return true;
}
return false;
}
static bool startswith(const std::string &a, const std::string &b) {
if (a.compare(0, b.size(), b) == 0) {
return true;
}
return false;
}
static std::vector<std::string> split(const std::string& s, const std::string delimiter=" ") {
std::string cp = s;
std::vector<std::string> vecStr;
size_t pos = 0;
std::string token;
while ((pos = cp.find(delimiter)) != std::string::npos) {
token = cp.substr(0, pos);
vecStr.push_back(token);
cp.erase(0, pos + delimiter.length());
}
vecStr.push_back(cp);
return vecStr;
}
static std::string strip(const std::string &str,char ch=' ')
{
//除去str两端的ch字符
int i = 0;
while (str[i] == ch)// 头部ch字符个数是 i
i++;
int j = str.size() - 1;
while (str[j] == ch ) //
j--;
return str.substr(i, j+1 -i );
}
static std::string lstrip(const std::string &str, char ch)
{
//除去str两端的ch字符
int i = 0;
while (str[i] == ch)// 头部ch字符个数是 i
i++;
int j = str.size() - 1;
return str.substr(i, j + 1 - i);
}
static std::string rstrip(const std::string &str, char ch)
{
//除去str两端的ch字符
int i = 0;
int j = str.size() - 1;
while (str[j] == ch ) //
j--;
return str.substr(i, j + 1 - i );
}
static std::string strip(const std::string &str)
{
//除去str两端的ch字符
int i = 0;
while (std::isspace(str[i]))// 头部ch字符个数是 i
i++;
int j = str.size() - 1;
while (std::isspace(str[j])) //
j--;
return str.substr(i, j+1 -i );
}
static std::string lstrip(const std::string &str)
{
//除去str两端的ch字符
int i = 0;
while (std::isspace(str[i]))// 头部ch字符个数是 i
i++;
int j = str.size() - 1;
return str.substr(i, j + 1 - i);
}
static std::string rstrip(const std::string &str)
{
//除去str两端的ch字符
int i = 0;
int j = str.size() - 1;
while (std::isspace(str[j])) //
j--;
return str.substr(i, j + 1 - i );
}
static std::string read_file(const std::string &name) {
std::ifstream ifs(name);
std::string content( (std::istreambuf_iterator<char>(ifs) ),
(std::istreambuf_iterator<char>() ) );
return content;
}
};