Question

How to Split a String by a String Delimiter in C++

cppparsingsplittokentokenize

Question

In standard C++, std::getline works well when the delimiter is a single character. For example:

using namespace std;

string parsed;
string input = "text to be parsed";
stringstream input_stringstream(input);

if (getline(input_stringstream, parsed, ' '))
{
    // do some processing
}

However, how can you split a string when the delimiter is a string instead of a single character?

For example, given this input:

string input = "scott>=tiger";

How can you split it using ">=" as the delimiter so that you get:

  • scott
  • tiger

Short Answer

By the end of this page, you will understand why std::getline cannot split on multi-character delimiters, and how to use std::string::find and std::string::substr to split strings using string delimiters like ">=" in C++. You will also see common patterns, mistakes, and a small practical project.

Concept

In C++, std::getline supports only a single character delimiter. That works for cases like splitting by a space, comma, or colon, but not for delimiters made of multiple characters such as ">=", "--", or "||".

When your delimiter is a string, the usual approach in standard C++ is:

  1. Search for the delimiter inside the string using std::string::find
  2. Extract the part before it using std::string::substr
  3. Extract the part after it using std::string::substr again

This matters because real programs often parse structured text that uses multi-character separators, such as:

  • configuration entries like key=>value
  • log data like time||message
  • simple query expressions like name>=john
  • custom file formats and protocols

Understanding this pattern is useful because find + substr is one of the most common standard-library techniques for string parsing in C++. It is simple, readable, and does not require non-standard libraries.

Mental Model

Think of a string as a long strip of paper.

  • find helps you locate where a marker appears on the strip
  • substr lets you cut out pieces of the strip

If the string is:

scott>=tiger

and your marker is:

>=

then you:

  1. find where ">=" starts
  2. cut everything before it → scott
  3. cut everything after it → tiger

So instead of saying “split by one character,” you are really saying “find this substring, then slice around it.”

Take Quiz

Syntax and Examples

Core idea

Use:

  • find(delimiter) to locate the delimiter
  • substr(start, length) to extract pieces

Example: split once by a string delimiter

#include <iostream>
#include <string>

int main()
{
    std::string input = "scott>=tiger";
    std::string delimiter = ">=";

    std::size_t pos = input.find(delimiter);

    if (pos != std::string::npos)
    {
        std::string left = input.substr(0, pos);
        std::string right = input.substr(pos + delimiter.length());

        std::cout << "Left: " << left << '\n';
        std::cout << "Right: " << right << '\n';
    }
    else
    {
        std::cout << "Delimiter not found\n";
    }
}

Output

Left: scott
Right: tiger

Why this works

  • find(delimiter) returns the starting index of ">="
  • substr(0, pos) gets everything before that index
  • substr(pos + delimiter.length()) gets everything after the delimiter

Example: delimiter appears in a larger string

#include <iostream>
#include <string>

int main()
{
    std::string input = "name>=scott";
    std::string delimiter = ">=";

    std::size_t pos = input.find(delimiter);

    if (pos != std::string::npos)
    {
        std::string field = input.substr(0, pos);
        std::string value = input.substr(pos + delimiter.size());

        std::cout << field << '\n';
        std::cout << value << '\n';
    }
}

This is a common pattern when parsing simple expressions or key-value pairs.

Step by Step Execution

Consider this code:

#include <iostream>
#include <string>

int main()
{
    std::string input = "scott>=tiger";
    std::string delimiter = ">=";

    std::size_t pos = input.find(delimiter);

    if (pos != std::string::npos)
    {
        std::string left = input.substr(0, pos);
        std::string right = input.substr(pos + delimiter.size());

        std::cout << left << '\n';
        std::cout << right << '\n';
    }
}

Step-by-step

1. Store the input

std::string input = "scott>=tiger";

The string contains 12 characters:

s c o t t > = t i g e r
0 1 2 3 4 5 6 7 8 9 10 11

2. Store the delimiter

std::string delimiter = ">=";

Its length is 2.

3. Find the delimiter position

std::size_t pos = input.find(delimiter);

pos becomes 5, because ">=" starts at index 5.

4. Extract the left part

std::string left = input.substr(0, pos);

This means:

  • start at index 0
  • take 5 characters

Result:

scott

5. Extract the right part

std::string right = input.substr(pos + delimiter.size());

This means:

  • start after the delimiter
  • pos + 2 = 7
  • take the rest of the string

Result:

tiger

6. Print the results

Output:

scott
tiger

Real World Use Cases

Multi-character delimiters appear more often than beginners expect.

Common uses

  • Parsing configuration lines
    • Example: host=>localhost
  • Processing simple expressions
    • Example: age>=18
  • Reading custom file formats
    • Example: header||body
  • Handling logs or exported data
    • Example: timestamp-->message
  • Tokenizing user input in small tools
    • Example: username::role

Practical example: parsing a filter rule

std::string rule = "price>=100";
std::string delimiter = ">=";
std::size_t pos = rule.find(delimiter);

if (pos != std::string::npos)
{
    std::string field = rule.substr(0, pos);
    std::string value = rule.substr(pos + delimiter.size());
}

This could be used in a search tool, reporting script, or command parser.

Take Quiz

Real Codebase Usage

In real codebases, developers usually wrap this logic in a reusable function instead of writing find and substr repeatedly.

Common pattern: return two parts

#include <string>
#include <utility>

std::pair<std::string, std::string> split_once(const std::string& input, const std::string& delimiter)
{
    std::size_t pos = input.find(delimiter);

    if (pos == std::string::npos)
    {
        return {input, ""};
    }

    return {
        input.substr(0, pos),
        input.substr(pos + delimiter.size())
    };
}

Guard clause pattern

A guard clause checks bad or special cases early:

if (delimiter.empty())
{
    // handle invalid delimiter
}

This is important because splitting by an empty delimiter is usually not meaningful.

Validation pattern

Developers often validate input before parsing:

  • Is the delimiter present?
  • Does the left side exist?
  • Does the right side exist?
  • Can the right side be converted to a number?

Error-handling pattern

Instead of assuming success, real programs often do this:

if (pos == std::string::npos)
{
    // report parse failure or use fallback behavior
}

Repeated splitting

If a string may contain the delimiter multiple times, developers usually use a loop with find to keep extracting tokens.

Common Mistakes

1. Trying to use std::getline with a string delimiter

This does not work because the third parameter must be a single char.

std::getline(stream, parsed, ">="); // wrong

Use find and substr instead.

2. Forgetting to check whether the delimiter exists

Broken code:

std::size_t pos = input.find(delimiter);
std::string left = input.substr(0, pos);
std::string right = input.substr(pos + delimiter.size());

If the delimiter is not found, pos is std::string::npos, which can lead to incorrect behavior.

Better:

std::size_t pos = input.find(delimiter);
if (pos != std::string::npos)
{
    std::string left = input.substr(0, pos);
    std::string right = input.substr(pos + delimiter.size());
}

3. Using the wrong starting index for the right part

Broken code:

std::string right = input.substr(pos + 1);

This only skips one character, but the delimiter ">=" has length 2.

Correct:

std::string right = input.substr(pos + delimiter.size());

4. Not handling an empty delimiter

Broken idea:

std::string delimiter = "";

find("") is valid, but usually not what you want for splitting.

Better:

if (delimiter.empty())
{
    // reject or handle specially
}

5. Assuming there is only one delimiter

For input like:

a>=b>=c

find returns the first match only. If you need all parts, use a loop.

6. Confusing size() and character positions

Remember:

  • pos is where the delimiter starts
  • delimiter.size() is how many characters it contains
  • the right part begins at pos + delimiter.size()

Comparisons

Single-character vs string delimiter parsing

ApproachDelimiter typeStandard C++ supportBest for
std::getline(stream, text, ',')Single characterYesSimple tokenizing from streams
find + substrString or characterYesSplitting by multi-character delimiters

find vs stringstream

ToolGood atLimitation
stringstream + getlineReading text pieces separated by one characterCannot directly use a string delimiter
std::string::findLocating substrings anywhere in a stringYou must manually extract pieces

Split once vs split many times

PatternUse whenExample
Split onceYou only need left and right sidesname>=scott
Split in a loopThe delimiter may appear multiple timesa>=b>=c

find vs regex

ToolBest forNotes
findFixed delimiters like ">="Simple and fast
regexComplex patternsUsually more complicated than needed for basic splitting
Take Quiz

Cheat Sheet

Quick reference

Split once by string delimiter

std::string input = "scott>=tiger";
std::string delimiter = ">=";

std::size_t pos = input.find(delimiter);
if (pos != std::string::npos)
{
    std::string left = input.substr(0, pos);
    std::string right = input.substr(pos + delimiter.size());
}

Important methods

  • find(str) → returns index of first match or std::string::npos
  • substr(start, length) → returns part of a string
  • substr(start) → returns from start to end
  • size() or length() → number of characters

Rules

  • std::getline only accepts a char delimiter
  • Use find for multi-character delimiters
  • Always check pos != std::string::npos
  • Skip the full delimiter with pos + delimiter.size()
  • Validate that the delimiter is not empty

Common pattern

if (!delimiter.empty())
{
    std::size_t pos = input.find(delimiter);
    if (pos != std::string::npos)
    {
        auto left = input.substr(0, pos);
        auto right = input.substr(pos + delimiter.size());
    }
}

Edge cases

  • Delimiter not found
  • Delimiter at the beginning
  • Delimiter at the end
  • Empty input string
  • Empty delimiter
  • Multiple occurrences of the delimiter

FAQ

Can std::getline use a string delimiter in C++?

No. The delimiter parameter for std::getline is a single char, not a std::string.

What is the standard C++ way to split by ">="?

Use std::string::find to locate the delimiter and std::string::substr to extract the parts before and after it.

What happens if the delimiter is not found?

find returns std::string::npos. You should check for that before calling substr based on the found position.

How do I split a string multiple times using the same delimiter?

Use a loop with find, extract the token, remove or skip the processed part, and continue until no delimiter remains.

Is find better than regular expressions for this task?

For a fixed delimiter like ">=", yes. It is simpler, easier to read, and usually more efficient.

Can I split by more than one possible delimiter?

Yes, but you need extra logic, such as checking several delimiters or using a more advanced parser.

What if the delimiter is at the start or end of the string?

You will get an empty left or right substring. That may be valid or may need validation depending on your program.

Take Quiz

Mini Project

Description

Build a small C++ parser that reads expressions in the form left>=right and separates them into two values. This demonstrates how to parse strings with a multi-character delimiter using only the standard library.

Goal

Create a program that splits an input string using a string delimiter and prints the left and right parts safely.

Requirements

  • Read or define an input string that contains a multi-character delimiter.
  • Use std::string::find to locate the delimiter.
  • Print the text before and after the delimiter.
  • Handle the case where the delimiter is missing.
  • Reject an empty delimiter.
Take Quiz

Keep learning

Related questions

View more