Splitting a string in programming is a frequent occurrence. When tackling various issues or enhancing program efficiency, developers often encounter the need to split strings. In C++, there are multiple techniques available to accomplish this task. Each method presents unique time and space complexities. This guide will explore several potential methods for string splitting based on delimiters.
Delimiters serve as markers that define the limits within a sentence. They play a crucial role in enhancing readability, especially in specific types of text. At times, delimiters are employed to convey specific nuances within sentences. Examples of delimiters include space, comma, question mark, colon, semicolon, hyphen, forward slash, underscore, tab, and exclamation marks.
Example 1:
Let's consider a few instances to illustrate the process of dividing a string using various delimiters.
String: Hurry up! Our victory in the game is a significant accomplishment.
Delimiters: !,.
Output:
Hurry
We won the match
It is an achievement
Example 2:
String: Have a nice day
Delimiter: space
Output:
Below are some of the different ways to split the strings.
Utilizing std::istringstream in conjunction with std::getline:
Let's consider an instance to showcase how to separate a string by various delimiters using the std::istringstream and std::getline functions in C++.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input, const string& delimiters) {
vector<string> tokens;
istringstream iss(input);
string token;
while (getline(iss, token)) {
size_t start = 0, end = 0;
while ((start = token.find_first_not_of(delimiters, end)) != string::npos) {
end = token.find_first_of(delimiters, start);
tokens.push_back(token.substr(start, end - start));
}
}
return tokens;
}
int main() {
string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
vector<string> tokens = splitString(input, ",.!?-");
for (const auto& token : tokens) {
cout << token << endl;
}
return 0;
}
Output:
Explanation:
This software will divide the fixed string by utilizing the separators stored in the array. In this case, the isstringstream is employed to loop through each term and employs the getline method to separate the term.
- Leveraging std::regex
Let's consider an illustration to showcase the process of dividing a string by various delimiters using the std::regex function in C++.
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input, const string& delimiters) {
regex re("[" + delimiters + "]+");
vector<string> tokens(sregex_token_iterator(input.begin(), input.end(), re, -1), sregex_token_iterator());
return tokens;
}
int main() {
string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
vector<string> tokens = splitString(input, ",.!-");
for (const auto& token : tokens) {
cout << token << endl;
}
return 0;
}
Output:
Explanation:
This software utilizes standard explanation to divide the string in C++. Typically, the separators are stored in an array, and regular expressions are employed to recognize these separators within the string for the purpose of segmentation.
- Employing std::string::findfirstof and std::string::substr:
Let's consider an illustration to showcase the process of dividing a string by various delimiters through the utilization of the std::string::findfirstof and std::string::substr functions in C++.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input, const string& delimiters) {
vector<string> tokens;
string token;
size_t start = 0, end = 0;
while ((end = input.find_first_of(delimiters, start)) != string::npos) {
token = input.substr(start, end - start);
if (!token.empty())
tokens.push_back(token);
start = end + 1;
}
token = input.substr(start);
if (!token.empty())
tokens.push_back(token);
return tokens;
}
int main() {
string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
vector<string> tokens = splitString(input, ",.!-");
for (const auto& token : tokens) {
cout << token << endl;
}
return 0;
}
Output:
Explanation:
This approach offers an alternative method for dividing the string. In this case, we cycle through the input.find function to identify the locations of the separators, and subsequently extract the various segments from the string. These substrings are then inserted into the resulting array, effectively eliminating the separators.
- Utilizing Boost.Tokenizer:
Let's consider an illustration to showcase the process of dividing a string by various delimiters utilizing the Boost.Tokenizer function in C++.
#include <iostream>
#include <string>
#include <vector>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
vector<string> splitString(const string& input, const string& delimiters) {
vector<string> tokens;
tokenizer<escaped_list_separator<char>> tknzr(input, escaped_list_separator<char>("", delimiters, ""));
for (const string& token : tknzr)
tokens.push_back(token);
return tokens;
}
int main() {
string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
vector<string> tokens = splitString(input, ",.!-");
for (const auto& token : tokens) {
cout << token << endl;
}
return 0;
}
Output:
Explanation:
This code utilizes the Boost.Tokenizer to divide the input, parsing the static string using delimiters defined in the delimiters vector. Initially, it instantiates the tokenizer object, then applies it to the string, resulting in the string being separated by various delimiters.
- Dividing the string based on spaces as the delimiter.
Let's consider an example to showcase how you can divide a string by various delimiters by utilizing a space as the delimiter in C++.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> splitString(const string& input) {
vector<string> tokens;
string currentToken;
for (char ch : input) {
if (ch == ' ') {
if (!currentToken.empty()) {
tokens.push_back(currentToken);
currentToken.clear();
}
} else {
currentToken += ch;
}
}
if (!currentToken.empty()) {
tokens.push_back(currentToken);
}
return tokens;
}
int main() {
string input = "wow! High waves are rising in blue ocean, while the moon shines bright. Isn't it amazing?";
vector<string> tokens = splitString(input);
for (const auto& token : tokens) {
cout << token << endl;
}
return 0;
}
Output:
Conclusion:
In summary, this guide will present several techniques for efficiently dividing strings. These techniques are applicable to C++. Unlike some other programming languages, C++ does not have a built-in function for splitting strings based on a delimiter. Therefore, these approaches are valuable when working with strings in C++ programming.