将字符串拆分为点,并在C++中提取所有字段
Split the string on dot and extract all the fields from it in C++?
我正在尝试在点上拆分我的实际键,然后在将其拆分为点后从中提取所有字段。
我的钥匙看起来像这样——
@event.1384393612958.1136580077.TESTING
目前,我只能提取在点上拆分后@event
的第一个字段。现在如何提取所有其他字段。下面是我的代码,我目前正在使用从中提取第一个字段。
if (key)
{
char* first_dot = strchr(key, '.');
if (first_dot)
{
// cut at the first '.' character
first_dot[0] = 0;
}
}
cout << "Fist Key: " << key << endl;
然后在提取单个字段后,将第二个字段存储为 uint64_t
,第三个字段存储为 uint32_t
,最后一个字段存储为 string
。
更新:-
这就是我现在得到的——
if (key)
{
char *first_dot = strtok(key, ".");
char *next_dot = strtok(NULL, ".");
uint64_t secondField = strtoul(next_dot, 0, 10);
cout << first_dot << endl;
cout << secondField << endl;
}
我能够从中提取第一个和第二个字段。第三和第四领域呢?
你可以
改用strtok
。 strtok
确实如此,你现在正在做的事情。当遇到分隔字符时,它会拆分字符串
if (key) {
char *first_dot = strtok(key, ".");
...
char *next_dot = strtok(NULL, ".");
}
转换为某些int
类型可以使用strtoul/strtoull
uint64_t i = strtoul(next_dot, 0, 10);
相关文章:
- 将结构字段的类型展开为可变模板参数
- 将位字段导出到数组
- 为了方便起见,我应该避免公开私有字段变量吗
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 在java中读取c++字节的位字段
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 如何创建一个函数来提取向量内部字符串中的字段?
- 如何安全地将有符号字段从uint32_t提取到有符号数字(int或uint32_t)中
- 使用 istream 提取运算符进行 csv 解析:如何检测缺失的字段值?
- 使用C 从文本行中提取字段和值
- 从CSV文件中提取每个条目字段的最佳方法是什么
- 在特定字段中关键字搜索后,从文本文件中提取多个记录
- 如何使用C/C++提取pcap文件的TCP标头中的选项字段
- 如何在点上拆分字符串并有效地提取所有字段
- 将字符串拆分为点,并在C++中提取所有字段
- 如何在C++中从字节数组(在 BIG-ENDIAN 中)中提取单个字段
- C++ libcurl IMAP 提取标头字段
- 我只需要从文件中提取每条记录的一个字段,但它不起作用
- 如何使用libcurl提取Location:HTTP头字段
- 如何从C++中字段由'|'字符分隔的行中提取数据?