How to Separate a String in C++
A collection of words or a set of words is divided into individual words when a string is split or divided. But only with certain delimiters such as white space( ), comma( ), hyphen( -), and so on. It is feasible to break strings into individual words.
The split() function is a feature of several programming languages that allows you to break a string into many sections. Although C++ lacks an innate split() method, several techniques can achieve the same result, including the use of the getline(), stroke (), find(), erase(), and other functions. This article has covered the use of these functions to divide strings in C++.
Various functions in C++ are used to separate a string:
- Using find(), substr() and erase() Functions
- Using Temporary String
- Using strtok() Function
- Using stringstream API of C++
- Using Custom split() Function
- Using std::regex_token_iterator Function
- Using find_first_not_of() with find() Function
- Using std::getline() Function
1. Using find(), substr() and erase() Functions:
A predefined C++ method called substr() is used to manage string operations. This method takes two parameters, len, and pos, and outputs a newly formed string object with a duplicate of one of the object's substrings as its value. Pos is the starting point for string copying, which continues until pos+len, or [pos, pos+len].
Two parameters are required by the substr() function:
- Position: This is where the initial variable is located.
- length: Specifies the desired substring's length.
Syntax:
string substr (size position, size length);
Example:
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> splitString(std::string& str, char delimiter)
{
std::vector<std::string> tokens;
size_t pos = 0;
while ((pos = str.find(delimiter)) != std::string::npos) {
std::string token = str.substr(0, pos);
tokens.push_back(token);
str.erase(0, pos + 1); // Erase the token including the delimiter
}
// Add the last token
tokens.push_back(str);
return tokens;
}
int main()
{
std::string input = "Hello,World,This,is,C++,programming";
char delimiter = ',';
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens)
{
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp1.png)
This method splits a string using the std::string class's locate(), substr(), and erase() capabilities. We use locate() to find the position of the delimiter, substr() to extract the substring that comes before it, and erase() to remove the token, including the delimiter from the original string. Until all delimiters are located and tokens are extracted, this process is repeated. One std::vector contains the extracted tokens. If maintaining the original string is necessary, proceed with caution, as the original string is altered during this operation.
2. Using Temporary String:
Another way to split a text based on a delimiter without changing the original string is to use a temporary string. This is a succinct description of this methodology:
Up to the delimiter, we append each character in the original string to a temporary string as we iterate through the characters. We clear the temporary string and add it to our tokens vector once we've located the delimiter. In this manner, the original string is preserved while the temporary string contains each token.
Example:
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::string temp;
for (char ch : str) {
if (ch != delimiter) {
temp += ch;
} else {
tokens.push_back(temp);
temp.clear();
}
}
// Add the last token if the string doesn't end with the delimiter
if (!temp.empty()) {
tokens.push_back(temp);
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
char delimiter = ',';
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp2.png)
This method iterates over the original string character by character, using a temporary string to contain each token. We clear the temporary string and add its current contents to our tokens vector when we come across the delimiter. With this method, we can divide the original string into tokens without changing it. If a token is still present in the temporary string after all characters have been processed, it is added to the tokens vector. This technique stores the tokens in a different vector while guaranteeing that the original string is preserved.
3. Using strtok() Function:
Depending on the given delimiter, the strtok() function splits the original string into chunks or tokens. Every time the strtok() method is called, a NULL character (\0) is inserted at the delimiter point to alter the original string. This allows it to track the status of the token easily.
Syntax
char *ptr = strtok (string, delimiter);
Parameters
Two parameters are required by the strtok() function:
- String: To split this text, we use the strtok() function on the original string.
- Delimiter: The character or parameter that is used to divide a string.
Return Type: A pointer to the following character tokens is returned. It first directs the user to the strings' first token.
Example:
#include <iostream>
#include <cstring>
#include <vector>
std::vector<std::string> splitString(const std::string& str, const char* delimiter) {
char* token = strtok(const_cast<char*>(str.c_str()), delimiter);
std::vector<std::string> tokens;
while (token != NULL) {
tokens.push_back(token);
token = strtok(NULL, delimiter);
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
const char* delimiter = ",";
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp3.png)
Tokenize a string based on a delimiter by substituting a null character for the delimiter using the strtok() function. It uses a static pointer to keep its state between calls and returns a pointer to the first token discovered in the original string. From where it left off, tokenizing the string is continued in subsequent calls to strtok() with NULL. Without changing the original string, we utilize this function to divide it into tokens. An array of std::vector contains each token. This method effectively divides the string into tokens without compromising the integrity of the original text.
4. Using stringstream API of C++:
The stringstream objects automatically tokenize strings that contain spaces and can be initialized with a string object. You can read a string as a stream of words using the stringstream, much like you can with the cin stream. The following stringstream operators are the most often used ones:
- A string object is pushed into the stream via operator\\
- A word is extracted from the stream with the operator>>
Example:
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
char delimiter = ',';
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp4.png)
Using the std::stringstream class, we can handle a string as an input stream. After initializing it with the original string, we extract tokens based on a delimiter using std::getline(). Every token in between delimiters is added to a std::vector as we loop through the string. Without changing the original string, this method effectively divides the string into tokens. It handles string tokenization by utilizing the stream-based operations of std::stringstream, offering a simple and orderly method of dividing the string into distinct tokens.
5. Using Custom split() Function:
Iterating through string character by character and gathering substrings in between delimiters is required to create a custom split() function. The various tokens will be contained in a std::vector that is returned by this function.
Example:
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> split(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::string token;
for (char ch : str) {
if (ch != delimiter) {
token += ch;
} else {
tokens.push_back(token);
token.clear();
}
}
if (!token.empty()) {
tokens.push_back(token);
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
char delimiter = ',';
std::vector<std::string> tokens = split(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp5.png)
Character by character, the original string is iterated by using the custom split() function. It keeps the accumulated characters as a token in a std::vector as it comes across the delimiter. Up to the string's conclusion, this process is repeated. The final token is made up of the remaining characters in the string if there isn't a delimiter at the end. Utilizing a std::string to gather characters and a std::vector to hold the tokens, the function effectively divides the string into tokens without altering it. This customized method offers a simple and understandable technique to divide strings according to a delimiter.
6. Using std::regex_token_iterator Function:
Every regular expression match in the underlying character sequence has its various sub-matches accessible by the read-only LegacyForwardIterator std::regex_token_iterator. Additionally, it can be used as a tokenizer, or to access sequence portions that the supplied regular expression did not match. The text is divided using the std::sregex_token_iterator, a subclass of the std::regex token iterator.
Note: A regular expression, sometimes referred to as a regex, is an expression made up of a group of characters that specify a particular search pattern. These expressions can be utilized with find or find/replace algorithms, string searching techniques, and other similar algorithms.
Example:
#include <iostream>
#include <vector>
#include <string>
#include <regex>
std::vector<std::string> splitString(const std::string& str, const std::string& delimiter) {
std::regex regexDelimiter(delimiter);
std::sregex_token_iterator it(str.begin(), str.end(), regexDelimiter, -1);
std::sregex_token_iterator end;
std::vector<std::string> tokens;
for (; it != end; ++it) {
tokens.push_back(*it);
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
std::string delimiter = ",";
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp6.png)
A string can be effectively split according to a regular expression pattern using the std::regex_token_iterator. As it goes over the original string, tokens are extracted in between each time the delimiter pattern appears. Onto a std::vector is inserted each token. With the versatility, this method offers in defining delimiter patterns with regular expressions, comprehensive string tokenization is made possible. By utilizing the regex capabilities of the standard library, it provides a strong and efficient method for splitting strings. This method allows for robust and flexible tokenization operations and is especially helpful when the delimiter pattern is complex or varies across distinct strings.
7. Using find_first_not_of() with find() Function:
The find_first_not_of() function searches the string for the first character that does not correspond to any of the characters given as arguments. If the search is successful, the index of the first character that does not match is returned; otherwise, string::npos is used.
Syntax
size_type string::find_first_not_of (const string& str, size_type IDX) const
Example:
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
size_t start = 0, end;
// Find the first character that is not the delimiter
while ((start = str.find_first_not_of(delimiter, start)) != std::string::npos) {
// Find the next occurrence of the delimiter
end = str.find(delimiter, start);
// Extract the token
tokens.push_back(str.substr(start, end - start));
// Move to the next character after the delimiter
start = end;
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
char delimiter = ',';
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp7.png)
size_type string::find_first_not_of (const string& str, size_type idx) const
We determine the start point of each token by locating the first character that is not the delimiter using the find_first_not_of() and find() functions. Next, we locate each token's end place by following the delimiter's next occurrence after the start position. Substr() is used to extract each token, which is then added to a std::vector. Without the need for extra libraries or intricate reasoning, this method effectively divides the string into tokens depending on delimiters, offering a versatile and straightforward solution for string tokenization.
8. Using std::getline() Function:
The getline() function, a C++ standard library function, reads an input stream and inserts the string into a vector string until delimiter characters are encountered. We can utilize the std::getline() function by importing the header file.
Syntax
getline(string, token, delimiter);
Three arguments are required for the getline() function:
- string: The original string is stored in this variable;
- token: It stores the string tokens that were taken out of the original string.
- Delimiter: The character or parameter that is used to divide a string.
Example:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
int main() {
std::string input = "Hello,World,This,is,C++,programming";
char delimiter = ',';
std::vector<std::string> tokens = splitString(input, delimiter);
for (const auto& token : tokens) {
std::cout << token << std::endl;
}
return 0;
}
Output:
![How to Separate a String in C++](https://static.tutorialandexample.com/cpp/how-to-separate-a-string-in-cpp8.png)
We extract tokens separated by a delimiter by iterating over the original string using std::getline() with a std::stringstream. Onto a std::vector is inserted each token. This approach offers a simple solution for string tokenization by effectively dividing the string into tokens according to the delimiter. Using the C++ Standard Library's std::stringstream and std::getline() methods, it provides a clear and efficient method without requiring other libraries or intricate reasoning. This approach offers flexibility in managing different string formats and is especially helpful when the delimiter is a single character.ver, every key has been used twice: once for bucket assignment and once more for cache memory loading following bucket sorting.language..