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"
?
Remove specific substring from string in C++ std::string::find() is used to find the starting index of a substring in the original string. std::string::erase() is used to erase the substring from the original string.
translate() is another method that can be used to remove a character from a string in Python. translate() returns a string after removing the values passed in the table. Also, remember that to remove a character from a string using translate() you have to replace it with None and not "" .
Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.
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");
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With