#pragma once
|
#pragma once
|
#include <string>
|
#include <locale>
|
#include <codecvt>
|
#include <iostream>
|
#include <sstream>
|
#include <iomanip>
|
#include <vector>
|
using namespace std;
|
|
class StringUtil {
|
|
public:
|
static string wstringToString(wstring wt) {
|
wstring_convert<codecvt_utf8<wchar_t>> conv;
|
string result = conv.to_bytes(wt);
|
return result;
|
}
|
|
static std::wstring Unescape(const std::string& input) {
|
std::wstring wresult;
|
for (size_t i = 0; i < input.length(); ) {
|
if (input[i] == '\\' && input[i + 1] == 'u') {
|
std::string code = input.substr(i + 2, 4);
|
wchar_t unicode = stoi(code, nullptr, 16);
|
wresult += unicode;
|
i += 6;
|
}
|
else {
|
wresult += input[i++];
|
}
|
}
|
return wresult;
|
}
|
|
|
static string to_string(double val,int precision = 2) {
|
std::stringstream stream;
|
stream << std::fixed << std::setprecision(precision) << val;
|
return stream.str();
|
}
|
|
static bool isNumber(const std::string& str) {
|
if (str.empty()) {
|
return false;
|
}
|
for (char c : str) {
|
if (!isdigit(c)) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
// doubleתΪ×Ö·û´®
|
static string toString(double value, int precision) {
|
std::ostringstream oss;
|
oss << std::fixed << std::setprecision(precision) << value;
|
return oss.str();
|
}
|
|
static std::vector<std::string> split(std::string str, std::string pattern) {
|
std::string::size_type pos;
|
std::vector<std::string> result;
|
str += pattern;//À©Õ¹×Ö·û´®ÒÔ·½±ã²Ù×÷
|
int size = str.size();
|
for (int i = 0; i < size; i++)
|
{
|
pos = str.find(pattern, i);
|
if (pos < size)
|
{
|
std::string s = str.substr(i, pos - i);
|
result.push_back(s);
|
i = pos + pattern.size() - 1;
|
}
|
}
|
return result;
|
}
|
|
|
};
|