I do not know how the top answer received so many votes that he did when the question clearly asked the question of how to get a line between two line separators and not a pair of characters .
If you want to do this, you need to consider the length of the line separator, as this will not be just one character.
Case 1: Both delimiters are unique:
Given the line _STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_ from which you want to extract _0_192.168.1.18_ , you can change the top answer to get the desired effect. This is the simplest solution without introducing additional dependencies (e.g. Boost):
#include <iostream> #include <string> std::string get_str_between_two_str(const std::string &s, const std::string &start_delim, const std::string &stop_delim) { unsigned first_delim_pos = s.find(start_delim); unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length(); unsigned last_delim_pos = s.find(stop_delim); return s.substr(end_pos_of_first_delim, last_delim_pos - end_pos_of_first_delim); } int main() { // Want to extract _0_192.168.1.18_ std::string s = "_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_"; std::string s2 = "ABC123_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_XYZ345"; std::string start_delim = "_STARTDELIMITER"; std::string stop_delim = "STOPDELIMITER_"; std::cout << get_str_between_two_str(s, start_delim, stop_delim) << std::endl; std::cout << get_str_between_two_str(s2, start_delim, stop_delim) << std::endl; return 0; }
Will print _0_192.168.1.18_ twice.
You need to add the position of the first separator in the second argument to std::string::substr as last - (first + start_delim.length()) to make sure that it will still correctly extract the desired internal string if the separator does not start located at the very beginning of the line, as shown in the second case above.
See the demo .
Case 2: unique first separator, unique second separator:
Suppose you want a string between a unique delimiter and the first non-unique delimiter that occurs after the first delimiter. You can change the get_str_between_two_str function above to use find_first_of instead to get the desired effect:
std::string get_str_between_two_str(const std::string &s, const std::string &start_delim, const std::string &stop_delim) { unsigned first_delim_pos = s.find(start_delim); unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length(); unsigned last_delim_pos = s.find_first_of(stop_delim, end_pos_of_first_delim); return s.substr(end_pos_of_first_delim, last_delim_pos - end_pos_of_first_delim); }
If instead you want to capture any characters between the first unique delimiter and the last second delimiter encountered, for example, as commented above, use find_last_of .
Case 3: Minor first delimiter, unique second delimiter:
Very similar to case 2, just change the logic between the first separator and the second separator.
Case 4: Both delimiters are not unique:
Again, very similar to case 2, create a container to capture all the lines between any two delimiters. Scroll through the line and update the first position of the separator so that it is equal to the second position of the separator when it occurs, and add a line between the container. Repeat until std::string:npos .