fork(1) download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. std::string extract(std::string& str, char beg, char end)
  5. {
  6. std::size_t begPos ;
  7. if ( (begPos = str.find(beg)) != std::string::npos )
  8. {
  9. std::size_t endPos ;
  10. if ( (endPos = str.find(end, begPos)) != std::string::npos )
  11. {
  12. std::string result = str.substr(begPos+1, endPos-begPos-1) ;
  13. str.erase(begPos, endPos-begPos+1) ;
  14. return result ;
  15. }
  16. }
  17.  
  18. return std::string() ;
  19. }
  20.  
  21. int main()
  22. {
  23. std::string original("This is the string of text $Iwantthis+$Iwantthistoo text, end of the string.") ;
  24. std::cout << extract(original, '$', '+') << '\n' ;
  25. std::cout << extract(original, '$', ' ') << '\n' ;
  26. std::cout << "Original modified to \"" << original << "\"\n" ;
  27. }
  28.  
  29.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
Iwantthis
Iwantthistoo
Original modified to "This is the string of text text, end of the string."