C++ Basics: Split Text on a Multi-Character Delimiter
Use a string_view delimiter with views::split to separate fields on an entire token and preserve empty fields.
Prepare a scratch directory
Tested on Linux x86_64 with GCC 14.2.0. Install a C++20-capable compiler and standard-library development headers, with g++ on PATH. You also need Bash, a text editor, and the mktemp, rm, and rmdir utilities. No third-party library is required.
Keep the same terminal open. Stop if setup fails, before saving the source:
start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1Run the example
Save this as split-view-multichar-delimiter-demo.cpp:
#include <iostream>
#include <ranges>
#include <string>
#include <string_view>
int main()
{
using namespace std::string_view_literals;
const std::string input{"alpha::::beta::"};
for (auto field : input | std::views::split("::"sv)) {
const std::string copy(field.begin(), field.end());
std::cout << '[' << copy << "]\n";
}
}Compile and run it. The program runs only after compilation succeeds:
g++ -std=c++20 -Wall -Wextra -Werror -pedantic split-view-multichar-delimiter-demo.cpp -o split-view-multichar-delimiter-demo &&
./split-view-multichar-delimiter-demoObserved output:
[alpha]
[]
[beta]
[]Read the result
The adjacent delimiters produce an empty field between alpha and beta. The trailing delimiter produces another empty field. Each printed string is an owning copy of one subrange.
Apply it carefully
A raw string-literal array includes its terminating null; using the sv suffix excludes that null from the delimiter. The source remains alive throughout iteration. Subranges refer to source storage, so copy a field when it must outlive the source. This is delimiter splitting, not CSV parsing: quoted delimiters and escaping need a parser.
The C++ working draft describes the relevant rules. This example builds in C++20 mode; the current working draft also documents later additions.
Clean up
Remove only the source and executable created here, then return to the starting directory:
rm -- split-view-multichar-delimiter-demo.cpp split-view-multichar-delimiter-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directoryIf the directory is not empty, inspect its remaining contents before removing anything else.
Acknowledgements
This post was created with assistance from AI.