How to Parse String Using a Delimiter in C++

Jinku Hu Feb 02, 2024
  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
How to Parse String Using a Delimiter in C++

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 built-in 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::cin;
using std::cout;
using std::endl;
using std::istringstream;
using std::string;
using std::vector;

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 <sstream>
#include <string>
#include <vector>

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

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 <iterator>
#include <sstream>
#include <string>
#include <vector>

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

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;
}
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ String