字符串处理相关函数
复制代码
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
82
83
84
85
86
87
88//删除字符串开头的空格 void trim_left(string & str) { auto it = std::find_if_not(str.begin(), str.end(), isspace); str.erase(str.begin(),it ); } //删除字符串结尾的空格 void trim_right(string & str) { while (std::isspace(str.back())) { str.pop_back(); } } //删除字符串两端的空格 void trim(string & str) { trim_left(str); trim_right(str); } //用char字符分隔字符串str,分隔符只有一个 vector<string> split_by_char(const string & str, char delim, bool compress_token = true) { vector<string> ret; string tmp; for (auto it = str.begin(); it != str.end();++it) { if (*it != delim) { tmp.push_back(*it); } else { if (tmp.empty()) { if (compress_token == false) { ret.push_back(tmp); } } else { ret.push_back(tmp); tmp.clear(); } } } if (!tmp.empty()) { ret.push_back(tmp); } return ret; } //用delim中任意字符分隔字符串str,分隔符可支持多个 vector<string> split_by_str(const string & str,const string & delim, bool compress_token = true) { vector<string> ret; size_t start=0; size_t end=0; while (1) { end = str.find_first_of(delim,start); if (end == string::npos) { break; } string tmp = str.substr(start, end - start); if (tmp.empty()) { if (compress_token==false) { ret.emplace_back(std::move(tmp)); } } else { ret.emplace_back(std::move(tmp)); } start = end + 1; } string tmp = str.substr(start); if (!tmp.empty()) { ret.emplace_back(std::move(tmp)); } return ret; }
最后
以上就是个性棒棒糖最近收集整理的关于字符串处理相关函数的全部内容,更多相关字符串处理相关函数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复