-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathStlUtil.cc
65 lines (52 loc) · 1.26 KB
/
StlUtil.cc
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
#include "StlUtil.h"
#include <stdlib.h>
using namespace std;
namespace StlUtil
{
int Split(const string& str, char delim, vector<string>& out)
{
int c = 0;
stringstream ss(str);
string item;
while (getline(ss, item, delim))
{
++c;
out.push_back(item);
}
return c;
}
int Split(const string& str, const string& delim, vector<string>& out)
{
int co = 0;
size_t pos = 0;
size_t np = str.find(delim, 0);
while (np != string::npos && pos < str.size())
{
++co;
out.push_back(str.substr(pos, np - pos));
pos = np + delim.size();
np = str.find(delim, pos);
}
if (pos < str.size())
{
++co;
out.push_back(str.substr(pos));
}
return co;
}
string StringTrim(string& str, char ch)
{
string::size_type pos = str.find_last_not_of(ch);
if(pos != string::npos)
{
str.erase(pos + 1);
pos = str.find_first_not_of(ch);
if(pos != string::npos) str.erase(0, pos);
}
else
{
str = "";
}
return str;
}
}