分割字符串是比较常用的功能,自己基于 stl 实现了一个,也可以参考一下 QT 原生的字符串分割实现逻辑。
1. 轮子
- 源码。
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
// g++ -std='c++11' test_split_strings.cpp -o t && ./t
#include <iostream>
#include <string>
#include <list>
// 分割子串
std::list<std::string> split(const std::string& src, const std::string& sep) {
std::list<std::string> subs;
std::size_t pre = 0, cur = 0;
while ((pre = src.find_first_not_of(sep, cur)) != std::string::npos) {
cur = src.find(sep, pre);
if (cur == std::string::npos) {
subs.push_back(src.substr(pre, src.length() - pre));
break;
}
subs.push_back(src.substr(pre, cur - pre));
}
return subs;
}
// 去掉子串的前后空白符
void trim_empty_parts(std::list<std::string>& datas) {
for (auto it = datas.begin(); it != datas.end();) {
it->erase(0, it->find_first_not_of(" "));
it->erase(it->find_last_not_of(" ") + 1);
it = it->empty() ? datas.erase(it) : ++it;
}
}
int main() {
auto data = " ,127.0.0.1:6379, 127.0.0.1:6378 , 127.0.0.1:6377 ,3 ";
auto subs = split(data, ",");
for (auto& s : subs) {
std::cout << "len: " << s.length() << ", value: " << s << std::endl;
}
std::cout << "--------------" << std::endl;
trim_empty_parts(subs);
for (auto& s : subs) {
std::cout << "len: " << s.length() << ", value: " << s << std::endl;
}
return 0;
}
- 结果。
1
2
3
4
5
6
7
8
9
10
len: 1, value:
len: 14, value: 127.0.0.1:6379
len: 17, value: 127.0.0.1:6378
len: 17, value: 127.0.0.1:6377
len: 2, value: 3
--------------
len: 14, value: 127.0.0.1:6379
len: 14, value: 127.0.0.1:6378
len: 14, value: 127.0.0.1:6377
len: 1, value: 3
2. QT 原生
QT 原生字符串分割实现逻辑。
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
// C:\Qt\Qt5.14.2\5.14.2\Src\qtbase\src\corelib\text\qstring.cpp
QStringList QString::split(const QString& sep, SplitBehavior behavior,
Qt::CaseSensitivity cs) const {
return splitString<QStringList>(*this, sep.constData(), behavior, cs, sep.size());
}
namespace {
template <class ResultList, class StringSource>
static ResultList splitString(const StringSource& source, const QChar* sep,
QString::SplitBehavior behavior,
Qt::CaseSensitivity cs, const int separatorSize) {
ResultList list;
typename StringSource::size_type start = 0;
typename StringSource::size_type end;
typename StringSource::size_type extra = 0;
while ((end = QtPrivate::findString(
QStringView(source.constData(), source.size()), start + extra,
QStringView(sep, separatorSize), cs)) != -1) {
if (start != end || behavior == QString::KeepEmptyParts)
list.append(source.mid(start, end - start));
start = end + separatorSize;
extra = (separatorSize == 0 ? 1 : 0);
}
if (start != source.size() || behavior == QString::KeepEmptyParts)
list.append(source.mid(start, -1));
return list;
}
} // namespace