. Advertisement .
..3..
. Advertisement .
..4..
You can parse string using a delimiter in C++ without any third-party tools. The C++ standard library comes with many tools to accomplish this task. Explore these options and their examples below.
Parse String Using A Delimiter In C++
With std::stringstream and std::getline
This std class allows you to create input and output string streams that can be used in conjunction with other strings. They use a buffer to contain a sequence of characters, which you can access as a string object directly. You can insert and/or extract from this stream with operations typically used for input/output streams.
It can be combined with std::getline to extract characters in a stream until encountering a delimitation character. This function stores those characters in a string, and this extraction also ends when it reaches the end of file or due to other errors.
When std::getline recognizes the delimiter, the function extracts and discards it, meaning it won’t store the delimiter, and the next operation starts right after it.
Note that you should use an empty string for this because the getline()
function will replace its content with the extracted sequence. When a character is extracted, the function appends it at the end of the string in the same manner as push_back.
Example:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main()
{
std::string s = "ITTutoria-Stack Overflow-Quora";
const char delim = '-';
std::stringstream ss(s);
std::vector<std::string> out;
while (std::getline(ss, s, delim)) {
out.push_back(s);
}
for (auto &s: out) {
std::cout << s << std::endl;
}
return 0;
}
Output:
ITTutoria
Stack Overflow
Quora
The program above uses a dash as the delimitation character. The getline()
function extracts character after character from the stringstream sss until it runs into a dash. Note that it stores them in the original string s.
With std::string::find
The function find()
can search a string for the first occurrence of a substring. You can also specify a start position, making the function ignore any occurrences of the substring before that. Don’t use a position greater than the string length, or the function can’t find your substring.
Syntax:
size_t find (const char* s, size_t pos = 0) const;
- s: the pointer to the array of characters.
- pos: position of the start point of the search.
We can use the find()
function to search for the delimiter. The returned value is an unsigned integer type indicating the position of the first delimiter found on the string.
Example:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string s = "ITTutoria-Stack Overflow-Quora";
const char delim = '-';
std::vector<std::string> out;
size_t start;
size_t end = 0;
while ((start = s.find_first_not_of(delim, end)) != std::string::npos)
{
end = s.find(delim, start);
out.push_back(s.substr(start, end - start));
}
for (auto &s: out) {
std::cout << s << std::endl;
}
return 0;
}
Output:
ITTutoria
Stack Overflow
Quora
With std::regex_token_iterator
This iterator can access the parts of a sequence that match or don’t match a regular expression. You can use it to find every occurrence of the delimiter in the given string. Here is how one can rewrite the previous example using std::regex_token_iterator
.
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string s = "ITTutoria-Stack Overflow-Quora";
std::regex regex("\\-");
std::vector<std::string> out(
std::sregex_token_iterator(s.begin(), s.end(), regex, -1),
std::sregex_token_iterator()
);
for (auto &s: out) {
std::cout << s << std::endl;
}
return 0;
}
Output:
ITTutoria
Stack Overflow
Quora
Conclusion
You can parse string using a delimiter in C++ with many methods and functions in the std namespace. They all read your sequence of characters until the delimiter is found and store the result in a string. You can also create a string array, which is much easier to parse in C++. You can learn more about this with this guide.
Leave a comment