Java has split, C++ has ...?
The question on Day 2 of Advent of Code 2025, demanded splitting the inputs based on a delimiter and that's when I realized that unlike Java, C++ doesn't have an easy way to split the string. Thus began a journey to understand ways to split a string in C++, based on a string delimiter.
TLDR; The short answer use boost algorithms library! But if you're like me and sometimes enjoy reinventing the wheel, read on!
The Java Way!
Java has a simple and straightforward way to split a string, and it even supports regex patterns.
void splitDemo() {
String str = "This is a sample string.";
String[] splitStr = str.split(" "); // Split using space (' ') as a delimiter.
for (String s: splitStr) {
System.out.println(s);
}
}
Output:
This
is
a
sample
string.
Freedom in C++?
Unlike Java, C++ doesn't have any internal implementations of split hence, you have the "freedom" to implement your own split method or depend on an external library, such as boost.
1. stringstream with getline
Drawback: The getline method only supports a delimiter of type "char".
void
strsplit(const std::string str,
const char delimiter,
std::vector<std::string>& tokens) {
std::stringstream ss(str);
std::string tok;
while (std::getline(ss, tok, delimiter)) {
tokens.emplace_back(tok);
}
}
2. find with substr
Unlike the previous implementation, find() method supports finding a "substring" within a string, hence the delimiter can be a string.
void
strsplit(const std::string str,
const std::string delimiter,
std::vector<std::string>& tokens) {
std::size_t pos_increment = delimiter.length();
std::size_t start_idx = 0; // Starting position of current token (=0).
std::string tok;
while (true) {
// Search for the next occurence of the delimiter string starting from
// start_idx.
auto pos = str.find(delimiter, start_idx);
// If delimiter is not found, reached end of the string
if (pos == std::string::npos) {
// Extract the remaining substring and stop processing further.
tok = str.substr(start_idx);
if (tok.length() != 0) // Insert iff token is a non-empty substring.
tokens.emplace_back(tok);
break;
}
// Extract the substring from start_idx to the delimiter position
// (excluding the delimiter itself).
tok = str.substr(start_idx, pos - start_idx);
if (tok.length() != 0) // Insert iff token is a non-empty substring.
tokens.emplace_back(tok);
// Move start_idx past the current delimiter to ensure the delimiter is
// not included in the next token.
start_idx = pos + pos_increment;
}
}