How to Parse String Using a Delimiter in C++

  1. Use find() and substr() Methods to Parse String Using a Delimiter
  2. Use stringstream Class and getline Method to Parse String Using a Delimiter
  3. Use the copy() Function to Parse String by a Single Whitespace Delimiter

This article will explain how to parse a string by specifying a delimiter in C++.

Use find() and substr() Methods to Parse String Using a Delimiter

This method uses a builtin find method of the string class. It takes a sequence of characters to be found as a string type and the starting position as an integer parameter. If the method finds the passed characters, it returns the position of the first character. Otherwise, it returns npos. We put find statement in while loop to iterate over string until the last delimiter is found. To extract a substring between delimiters, substr function is used, and on each iteration, the token is pushed onto a words vector. As the last step of the loop, we remove the string’s processed part with the erase method.

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

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::istringstream;

int main(){
    string text = "He said. The challenge Hector heard with joy, "
               "Then with his spear restrain'd the youth of Troy ";
    string delim = " ";
    vector<string> words{};

    size_t pos = 0;
    while ((pos = text.find(delim)) != string::npos) {
        words.push_back(text.substr(0, pos));
        text.erase(0, pos + delim.length());
    }

    for (const auto &w : words) {
        cout << w << endl;
    }
    return EXIT_SUCCESS;
}

Output:

He
said.
The
...
Troy

Use stringstream Class and getline Method to Parse String Using a Delimiter

In this method, we are putting text string variable into a stringstream to operate on it with the getline method. getline extracts characters until the given char is found and stores the token in the string variable. Notice that this method can only be applied when a single character delimiter is needed.

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

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::stringstream;

int main(){
    string text = "He said. The challenge Hector heard with joy, "
               "Then with his spear restrain'd the youth of Troy ";
    char del = ' ';
    vector<string> words{};

    stringstream sstream(text);
    string word;
    while (std::getline(sstream, word, del))
        words.push_back(word);

    for (const auto &str : words) {
        cout << str << endl;
    }

    return EXIT_SUCCESS;
}

Use the copy() Function to Parse String by a Single Whitespace Delimiter

copy() is a <algorithm> library function, which can iterate through the specified range of elements and copy them to the destination range. At first, we initialize a istringstream variable with the text argument. After this, we utilize istream_iterator to loop over whitespace-separated substrings and finally output them to the console. Notice, though, this solution only works if the string needs to be split on whitespace delimiter.

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

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::istringstream;

int main(){
    string text = "He said. The challenge Hector heard with joy, "
               "Then with his spear restrain'd the youth of Troy ";

    istringstream iss(text);
    copy(std::istream_iterator<string>(iss),
         std::istream_iterator<string>(),
         std::ostream_iterator<string>(cout, "\n"));


    return EXIT_SUCCESS;
}

Related Article - C++ String

  • How to Convert Char Array to String in C++
  • How to Convert String to Lower Case in C++
  • comments powered by Disqus