Repository navigation
Expand file tree
/
Copy pathTextHelper.cpp
More file actions
81 lines (75 loc) · 2.67 KB
/
Copy pathTextHelper.cpp
File metadata and controls
81 lines (75 loc) · 2.67 KB
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
//
// Created by Administrator on 2023/5/1.
//
#include <sstream>
#include "TextHelper.h"
using namespace std;
namespace Netife{
std::vector <string> TextHelper::split(const std::string &str, const std::string &pattern) {
vector<string> res;
if(str.empty())
return res;
string strs = str + pattern;
size_t pos = strs.find(pattern);
while(pos != std::string::npos)
{
string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos+1, strs.size());
pos = strs.find(pattern);
}
return res;
}
std::vector<string> TextHelper::splitMulti(const string &str, const std::string &pattern){
vector<string> tokens;
size_t pos = 0;
std::string text(str);
string token;
while ((pos = text.find(pattern)) != string::npos) {
token = text.substr(0, pos);
text.erase(0, pos + pattern.length());
tokens.push_back(token);
}
tokens.push_back(text);
return tokens;
}
std::string TextHelper::getBetween(const string &str, const string &left, const string &right) {
string result = "";
size_t left_pos = str.find(left);
if (left_pos != string::npos) { // left字符串存在于s中
left_pos += left.length(); // left_pos调整为left后面的位置
size_t right_pos = str.find(right, left_pos); // 从left_pos右侧开始查找right字符串
if (right_pos != string::npos) { // right字符串存在于s中
result = str.substr(left_pos, right_pos - left_pos); // 取出left_pos与right_pos之间的子串
}
}
return result;
}
std::vector<std::string> TextHelper::splitByBlankWithSkipBlank(const string &str) {
vector<string> res;
stringstream ss(str);
string temp;
bool flag = false; // 判断是否在双引号内
while (ss >> temp) {
if (temp[0] == '\"') {
flag = true;
temp = temp.substr(1);
res.push_back(temp);
continue;
}
if (temp[temp.size()-1] == '\"') {
flag = false;
temp = temp.substr(0, temp.size()-1);
res.back() += ' ' + temp; // 注意需要加上空格
continue;
}
if (flag) {
res.back() += ' ' + temp; // 注意需要加上空格
} else {
res.push_back(temp);
}
}
return res;
}
}