fork(4) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::string get_str_between_two_str(std::string s, std::string start_delim,
  5. std::string stop_delim)
  6. {
  7. unsigned first_delim_pos = s.find(start_delim);
  8. unsigned end_pos_of_first_delim = first_delim_pos + start_delim.length();
  9. unsigned last_delim_pos = s.find(stop_delim);
  10.  
  11. return s.substr(end_pos_of_first_delim,
  12. last_delim_pos - end_pos_of_first_delim);
  13. }
  14.  
  15. int main() {
  16. // Want to extract _0_192.168.1.18_
  17. std::string s = "_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_";
  18. std::string s2 = "ABC123_STARTDELIMITER_0_192.168.1.18_STOPDELIMITER_XYZ345";
  19.  
  20. std::string start_delim = "_STARTDELIMITER";
  21. std::string stop_delim = "STOPDELIMITER_";
  22.  
  23. std::cout << get_str_between_two_str(s, start_delim, stop_delim) << std::endl;
  24. std::cout << get_str_between_two_str(s2, start_delim, stop_delim) << std::endl;
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
_0_192.168.1.18_
_0_192.168.1.18_