-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtil.h
More file actions
37 lines (32 loc) · 968 Bytes
/
StringUtil.h
File metadata and controls
37 lines (32 loc) · 968 Bytes
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
#include <vector>
#include <string>
using namespace std;
class StringUtil
{
public:
StringUtil() {}
~StringUtil() {}
public:
static std::vector<std::string> split(const std::string& text, const std::string& sepStr, bool ignoreEmpty = true);
};
std::vector<std::string> StringUtil::split(const std::string& text, const std::string& sepStr, bool ignoreEmpty) {
std::vector<std::string> vec;
std::string str(text);
std::string sep(sepStr);
size_t n = 0, old = 0;
while (n != std::string::npos)
{
n = str.find(sep,n);
if (n != std::string::npos)
{
if (!ignoreEmpty || n != old)
vec.push_back(str.substr(old, n-old));
n += sep.length();
old = n;
}
}
if (!ignoreEmpty || old < str.length()) {
vec.push_back(str.substr(old, str.length() - old));
}
return vec;
}