C ++ Get String Between Two String Separators

Is there a built-in function available for two lines between two lines of separator in C / C ++?

My entry looks like

_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_ 

And my conclusion should be

 _0_192.168.1.18_ 

Thanks in advance...

+6
source share
7 answers

You can do this:

 string str = "STARTDELIMITER_0_192.168.1.18_STOPDELIMITER"; unsigned first = str.find(STARTDELIMITER); unsigned last = str.find(STOPDELIMITER); string strNew = str.substr (first,last-first); 

Given that your STOPDELIMITER will only occur once at the end.

EDIT:

As a delimiter can occur several times, change your statement to find STOPDELIMITER for:

 unsigned last = str.find_last_of(STOPDELIMITER); 

This will give you text between the first STARTDELIMITER and LAST STOPDELIMITER, even though they are repeated several times.

+24
source

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 .

+7
source

I hope you do not mind, I answer another question :) I would use boost :: split or boost :: split_iter. http://www.boost.org/doc/libs/1_54_0/doc/html/string_algo/usage.html#idp166856528

For example, see the code in this SO question: How to avoid empty tokens when splitting with boost :: iter_split?

+1
source

To get a line between two lines of delimiter without spaces.

 string str = "STARTDELIMITER_0_192.168.1.18_STOPDELIMITER"; string startDEL = "STARTDELIMITER"; // this is really only needed for the first delimiter string stopDEL = "STOPDELIMITER"; unsigned firstLim = str.find(startDEL); unsigned lastLim = str.find(stopDEL); string strNew = str.substr (firstLim,lastLim); //This won't exclude the first delimiter because there is no whitespace strNew = strNew.substr(firstLim + startDEL.size()) // this will start your substring after the delimiter 

I tried combining two subscript functions, but started typing STOPDELIMITER

Hope that helps

+1
source

Let's say you need to get the 5th argument (brand) from the file below:

 zoneid:zonename:state:zonepath:uuid:brand:ip-type:r/w:file-mac-profile 

You cannot use any function "str.find" because it is in the middle, but you can use "strtok". eg.

 char *brand; brand = strtok( line, ":" ); for (int i=0;i<4;i++) { brand = strtok( NULL, ":" ); } 
+1
source

This is a late answer, but it might work too:

 string strgOrg= "STARTDELIMITER_0_192.168.1.18_STOPDELIMITER"; string strg= strgOrg; strg.replace(strg.find("STARTDELIMITER"), 14, ""); strg.replace(strg.find("STOPDELIMITER"), 13, ""); 

Hope this works for others.

+1
source
 void getBtwString(std::string oStr, std::string sStr1, std::string sStr2, std::string &rStr) { int start = oStr.find(sStr1); if (start >= 0) { string tstr = oStr.substr(start + sStr1.length()); int stop = tstr.find(sStr2); if (stop >1) rStr = oStr.substr(start + sStr1.length(), stop); else rStr ="error"; } else rStr = "error"; } 

or if you are using Windows and have access to C ++ 14, follow these steps:

 void getBtwString(std::string oStr, std::string sStr1, std::string sStr2, std::string &rStr) { using namespace std::literals::string_literals; auto start = sStr1; auto end = sStr2; std::regex base_regex(start + "(.*)" + end); auto example = oStr; std::smatch base_match; std::string matched; if (std::regex_search(example, base_match, base_regex)) { if (base_match.size() == 2) { matched = base_match[1].str(); } rStr = matched; } } 

Example:

  string strout; getBtwString("it's_12345bb2","it's","bb2",strout); getBtwString("it's_12345bb2"s,"it's"s,"bb2"s,strout); // second solution 

Headers:

 #include <regex> // second solution #include <string.h> 
0
source

Source: https://habr.com/ru/post/953820/


All Articles