C++ – How to remove a particular substring from a string


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"?

Best Solution

You can use erase for removing symbols:

str.erase(start_position_to_erase, number_of_symbols);

And you can use find to find the starting position:

start_position_to_erase = str.find("smth-to-delete");