1

I am working on a simple CSV parser.

From my csv file I get first row as a string, let's say:

"117;'Tom';'Sawyer';";

What I want to achieve is a function that breaks my string into pieces, similar to PHP's explode:

$string = "117;'Tom';'Sawyer';";
$row = explode(";", $string);
echo $row[0];

I need a function that would return an array of strings in the row variable.

I am new to C++ so I am unsure what to look for or use.

StarPilot
  • 2,246
  • 1
  • 16
  • 18
user2127071
  • 23
  • 1
  • 3
  • possible duplicate of [Is there an equivalent in C++ of PHP's explode() function?](http://stackoverflow.com/questions/12966957/is-there-an-equivalent-in-c-of-phps-explode-function) – Jesse Good Apr 03 '13 at 18:48
  • Potential duplicate of http://stackoverflow.com/questions/236129/splitting-a-string-in-c – Martin Apr 03 '13 at 18:48

2 Answers2

0

This is quite a common problem and one that you could have found with relative ease had you searched for it. Here's something that might help:

https://stackoverflow.com/a/236803/1974937

Community
  • 1
  • 1
stellarossa
  • 1,730
  • 1
  • 18
  • 29
0

It seems like you are looking for a function that splits the string using some specified delimiters and put them in a sequential container.

Here is a function that does that:

#include <iostream>
#include <string>
#include <vector>
#include <iterator>

/// Splits the string using provided delimiters and puts the pieces into a container.
/// The container must provide push_back and clear methods.
/// @param a The contaner to put the resulting substrings into
/// @param str The string to operate on
/// @param delims Characters that are to be treated as delimiters
/// @param compress_delims If set to true, will treat mutiple sequential delimiters as a single one
template<class StringType, class ContainerType>
void split_string(ContainerType& a, const StringType& str, const StringType& delims, bool compress_delims = true)
{
        typename StringType::size_type search_from = 0; // Place to start looking for delimiters
        typename StringType::size_type next_delim;      // Location of the next delimiter

        a.clear(); // Wipe out previous contents of the output container (it must be empty if the input string is empty)

        // Find the first delim after search_from,
        // add the substring between search_from and delimiter location to container,
        // update search_from to delimiter location + 1 so that next time we search,
        // we encounter the next delimiter. Repeat until we find the last delimiter.
        while((next_delim = str.find_first_of(delims, search_from)) != StringType::npos) {
                // If we encounter multiple delimiters in a row and compress_delims is true
                // treat it as a single delim.
                if(!(compress_delims && next_delim - search_from <= 1)){
                        StringType token = str.substr(search_from, next_delim - search_from);
                        a.push_back(token);
                }
                search_from = next_delim + 1;
        }

        // If we found the last delimiter and there are still some chars after it,
        // just add them to the container.
        if(search_from < str.length())
                a.push_back(str.substr(search_from));
}

int main()
{
        std::vector<std::string> container;
        std::string str = " hello so long    good  bye hurray    ";
        split_string(container, str, std::string(" "));
        std::copy(container.begin(), container.end(), std::ostream_iterator<std::string>(std::cout, ","));
        std::cout <<  " (" << container.size() << ")" << std::endl;
        return 0;
}

However, if it's possible to use Boost in your project, I'd advise you to do that. Use the boost.string_algo library, which contains a split function for that specific purpose (example usage).