-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
70 lines (61 loc) · 1.83 KB
/
Utils.cpp
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
#include "Utils.h"
#include <string>
#include <vector>
#include <algorithm>
// 将字符串根据指定pattern切分,放到vector里面
std::vector<std::string> SplitStrWithPattern(const std::string& str, const std::string& pattern)
{
std::vector<std::string> resVec;
if (str.empty())
return resVec;
std::string strs = str + pattern;
size_t pos = strs.find(pattern);
size_t size = strs.size();
while (pos != std::string::npos) {
std::string x = strs.substr(0, pos);
resVec.push_back(x);
strs = strs.substr(pos + pattern.size(), size);
pos = strs.find(pattern);
}
return resVec;
}
// string替换
std::string& ReplaceStr(std::string& str, const std::string& oldValue, const std::string& newValue)
{
for (std::string::size_type pos(0); pos != std::string::npos; pos += newValue.length()) {
if ((pos = str.find(oldValue, pos)) != std::string::npos)
str.replace(pos, oldValue.length(), newValue);
else
break;
}
return str;
}
// 判断string是否是数字
bool isNumeric(const std::string &str)
{
return !str.empty() && std::all_of(str.begin(), str.end(), ::isdigit);
}
// 判断字符串是否由start开头
bool StartsWith(const std::string& str, const std::string& start)
{
size_t srcLen = str.size();
size_t startLen = start.size();
if (srcLen >= startLen) {
std::string temp = str.substr(0, startLen);
if (temp == start)
return true;
}
return false;
}
// 判断字符串是否由end结尾
bool EndsWith(const std::string& str, const std::string& end)
{
size_t srcLen = str.size();
size_t endLen = end.size();
if (srcLen >= endLen) {
std::string temp = str.substr(srcLen - endLen, endLen);
if (temp == end)
return true;
}
return false;
}