admin
2024-07-05 3ef188e6075649f4c72e3e7588d8966e1071f2ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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;
    }
 
 
};