1

我的命令是:

move 1 "South Africa" "Europe"

代码:

do 
{
  cut = text.find(' ');
  if (cut == string::npos) 
  {
    params.push_back(text);
  } 
  else 
  {
    params.push_back(text.substr(0, cut));
    text = text.substr(cut + 1);
  }
}  
while (cut != string::npos);

问题是它South Africa正在分裂成Southand Africa,我需要它保留South Africa

切割后参数:

1, South, Africa, Europe

我需要它是:

1, South Africa, Europe

我怎样才能做到这一点?用正则表达式?

另一个命令示例:

move 3 "New Island" "South Afrika"

我的代码在''之后被删减,我需要在我推回的参数中

3, New Island, South Africa

我的代码使:

3,"New,Island","South,Africa"
4

1 回答 1

1

std::stringstream您可以使用和解析您的字符串std::getline

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string text("move 3 \"New Island\" \"South Afrika\"");
    std::string command, count, country1, country2, temp;
    std::stringstream ss(text);

    ss >> command >> count;
    ss.str("");
    ss << text;
    std::getline(ss, temp, '\"');
    std::getline(ss, country1, '\"');
    std::getline(ss, temp, '\"');
    std::getline(ss, country2, '\"');

    std::cout << command << ", " << count << ", " <<
        country1 << ", " << country2 << std::endl;
    return 0;
}
于 2013-06-19T00:34:05.750 回答