简体   繁体   中英

Splitting a string with different delimiters

Having the following type of input:

add name, breed, birthDate, vaccinationsCount, photograph

(eg add boo, yorkshire terrier, 01-13-2017, 7, boo puppy.jpg )

I want to split this string to get my parameters out of it and it didn't work.

My code looked like this:

getline(cin, listOfCommands);
string functionToApply = listOfCommands.substr(0, listOfCommands.find(" "));
int position = listOfCommands.find(" ");
listOfCommands.erase(0, position + 1);
cout << listOfCommands;
if (functionToApply == "exit")
    break;
else if (functionToApply == "add")
{
    position = listOfCommands.find(", ");
    string name = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 1);
    position = listOfCommands.find(", ");
    string breed = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 2);
    position = listOfCommands.find(", ");
    string birthDate = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 2);
    position = listOfCommands.find(", ");
    string nrShorts = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 2);
    string photo = listOfCommands;
}

Can someone help me, please?

For this sample I'm using std::getline with custom delimiter, std::stringstream to help parse the inputed stream and std::vector to store the parameters, (if you prefer you can assign them to the variables you created for them):

Live sample

#include <iostream>
#include <sstream>
#include <vector>

int main() {

    std::string listOfCommands, temp, command;
    std::vector<std::string> args; //container for the arguments

    //retrieve command
    getline(std::cin, command, ' ');

    if (command == "add") {

        getline(std::cin, listOfCommands);     
        std::stringstream ss(listOfCommands);

        while (getline(ss, temp, ',')) { //parse comma separated arguments

            while (*(temp.begin()) == ' ')
                temp.erase(temp.begin()); //remove leading blankspaces

            args.push_back(temp); // add parameter to container
        }

        //test print
        for (std::string str : args){
            std::cout << str << std::endl;
        }
    }
    return 0;
}

Input:

add boo, yorkshire terrier, 01-13-2017, 7,    boo puppy.jpg

Output:

boo
yorkshire terrier
01-13-2017
7
boo puppy.jpg

Try regex_token_iterator :

#include <regex>

const int split_constant = -1;
std::vector<std::string> args(
  std::sregex_token_iterator(listOfCommands.begin(), 
                             listOfCommands.end(), 
                             std::regex(", "), 
                             split_constant),
  std::sregex_token_iterator());

Of course you don't have to save the tokens in a vector, you can also just iterate over them:

auto iter = std::sregex_token_iterator(listOfCommands.begin(), 
                                       listOfCommands.end(), 
                                       std::regex(", "), 
                                       split_constant);
const string functionToApply = *iter++;
if (functionToApply == "exit") break;
const string name  = *iter++;
const string breed = *iter++;
// etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM