How to remove a particular substring from a string?

c++, string, substring

Solution

How about:

// Check if the last three characters match the ext.
const std::string ext(".gz");
if ( s != ext &&
     s.size() > ext.size() &&
     s.substr(s.size() - ext.size()) == ".gz" )
{
   // if so then strip them off
   s = s.substr(0, s.size() - ext.size());
}

Problem

In my C++ program, I have the string ``` string s = "/usr/file.gz"; ``` Here, how to make the script to check for `.gz` extention (whatever the file name is) and split it like `"/usr/file"`?

Original source

Related problems