std::string 二级分隔字符串,Cookie字符串分隔
分隔字符串的使用场景很多,所以一定要至少掌握一种适合自己的处理方法。c++中的std::string类,使用我们操作字符相对简单了很多。
下面实例是处理Cookie字符串的场景,源码如下:
0 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 |
/** * * @Author: mrdede <1234567@qq.com> * @Blog: http://mrdede.com/ * @Time: 2021/1/7 13:32 * */ #include <iostream> #include <cstring> int main() { std::string cookieString = "user=mrdede; web=mrdede.com; email=1234567@qq.com"; std::cout << "字符串总数:\t" << cookieString.length() << std::endl; int index = 0; bool is_end = false; std::string delim1 = "="; std::string delim2 = "; "; std::string tmp, key, value; // it.operator->() 同 it.base(),剩余未读取的字符 for(std::string::iterator it = cookieString.begin(); it != cookieString.end(); it++) { std::string str_tmp1, str_tmp2; str_tmp1 = it.operator*(); str_tmp2 = it.operator[](1); std::cout <<"\t"<< index << ":\t" << str_tmp1; std::cout << "\t" << str_tmp2; std::cout << "\t" << sizeof(str_tmp1); std::cout << "\t" << sizeof(it.operator*()); std::cout <<"\t"<<it.base() << std::endl; index++; // is_end = true,表示现在是最后一个字符了 if(it+1 == cookieString.end()) is_end = true; if(str_tmp1 == delim1) { // 获取 KEY key = tmp; tmp.clear(); } else if(str_tmp1 + str_tmp2 == delim2 || is_end) { // 获取 VALUE if(is_end) {// 当前是最后一个字符了 tmp += it.operator*(); } else { // 跳过下一个空格字符 it++; } value = tmp; // 插入业务处理逻辑 std::cout << "key: " << key <<", value: "<< value <<std::endl; key.clear(); value.clear(); tmp.clear(); } else { // 连接字符串 tmp += it.operator*(); } } return 0; } |
输出:
0 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 |
$ g++ main.cpp $ ./a.exe 字符串总数: 49 0: u s 8 1 user=mrdede; web=mrdede.com; email=1234567@qq.com 1: s e 8 1 ser=mrdede; web=mrdede.com; email=1234567@qq.com 2: e r 8 1 er=mrdede; web=mrdede.com; email=1234567@qq.com 3: r = 8 1 r=mrdede; web=mrdede.com; email=1234567@qq.com 4: = m 8 1 =mrdede; web=mrdede.com; email=1234567@qq.com 5: m r 8 1 mrdede; web=mrdede.com; email=1234567@qq.com 6: r d 8 1 rdede; web=mrdede.com; email=1234567@qq.com 7: d e 8 1 dede; web=mrdede.com; email=1234567@qq.com 8: e d 8 1 ede; web=mrdede.com; email=1234567@qq.com 9: d e 8 1 de; web=mrdede.com; email=1234567@qq.com 10: e ; 8 1 e; web=mrdede.com; email=1234567@qq.com 11: ; 8 1 ; web=mrdede.com; email=1234567@qq.com key: user, value: mrdede 12: w e 8 1 web=mrdede.com; email=1234567@qq.com 13: e b 8 1 eb=mrdede.com; email=1234567@qq.com 14: b = 8 1 b=mrdede.com; email=1234567@qq.com 15: = m 8 1 =mrdede.com; email=1234567@qq.com 16: m r 8 1 mrdede.com; email=1234567@qq.com 17: r d 8 1 rdede.com; email=1234567@qq.com 18: d e 8 1 dede.com; email=1234567@qq.com 19: e d 8 1 ede.com; email=1234567@qq.com 20: d e 8 1 de.com; email=1234567@qq.com 21: e . 8 1 e.com; email=1234567@qq.com 22: . c 8 1 .com; email=1234567@qq.com 23: c o 8 1 com; email=1234567@qq.com 24: o m 8 1 om; email=1234567@qq.com 25: m ; 8 1 m; email=1234567@qq.com 26: ; 8 1 ; email=1234567@qq.com key: web, value: mrdede.com 27: e m 8 1 email=1234567@qq.com 28: m a 8 1 mail=1234567@qq.com 29: a i 8 1 ail=1234567@qq.com 30: i l 8 1 il=1234567@qq.com 31: l = 8 1 l=1234567@qq.com 32: = 3 8 1 =1234567@qq.com 33: 3 4 8 1 1234567@qq.com 34: 4 4 8 1 234567@qq.com 35: 4 4 8 1 34567@qq.com 36: 4 0 8 1 4567@qq.com 37: 0 5 8 1 567@qq.com 38: 5 6 8 1 67@qq.com 39: 6 @ 8 1 7@qq.com 40: @ q 8 1 @qq.com 41: q q 8 1 qq.com 42: q . 8 1 q.com 43: . c 8 1 .com 44: c o 8 1 com 45: o m 8 1 om 46: m 8 1 m key: email, value: 1234567@qq.com |