1

我正在尝试解析这一行

Completion_Time_Stamp = 2013-04-04@12:10:22(Eastern Daylight Time)

并将名称放在一个变量中,将值放在另一个变量中

token[0] = strtok(buf, " = "); // first token

if (token[0]) // zero if line is blank
{
  for (n = 1; n < 10; n++)
  {
    token[n] = strtok(0, " = "); // subsequent tokens
    if (!token[n]) break; // no more tokens
  }
}

输出 :

token[0] = Completion_Time_Stamp
token[1] = 2013-04-04@12:10:22(Eastern
token[2] = Daylight
token[3] = Time)

但我想要这样的东西:

token[0] = Completion_Time_Stamp
token[1] = 2013-04-04@12:10:22(Eastern Daylight Time)

我该如何做到这一点?多个分隔符??

4

3 回答 3

4

为什么不使用已经存在的功能std::string,比如使用findand substr

就像是:

std::string str = "Completion_Time_Stamp = 2013-04-04@12:10:22(Eastern Daylight Time)";

auto delim_pos = str.find('=');

std::string keyword = str.substr(0, delim_pos);
std::string data = str.substr(delim_pos);

注意:delim_pos在制作子字符串时,可能需要调整分隔符位置(在我的示例中)。

于 2013-04-04T16:55:40.367 回答
1

您应该将=其用作分隔符,然后右修剪和左修剪您的结果,以消除标记 0 上的尾随右空格和标记 1 上的左空格。

于 2013-04-04T16:59:51.000 回答
0

使用 std::string,使用参数“=”查找,然后使用子字符串获取这两个字符串。

std::string str1 ="Completion_Time_Stamp = 2013-04-04@12:10:22(Eastern Daylight Time)";
std::string str2 = "=";
std::string part1="";
std::string part2="";

unsigned found = str1.find(str2);
if (found!=std::string::npos) 
{
   part1 = str1.substr(0,found-1);
   part2 = str1.substr(found+2);
}

cout << part1 << "\n" << part2 <<endl;

我得到以下信息:

Completion_Time_Stamp//^^no space here and beginning of second part
2013-04-04@12:10:22(Eastern Daylight Time)
于 2013-04-04T16:57:25.047 回答