Some others have asked about
However, it has not yet been asked how to remove a prefix or a suffix from a string in c++. Assuming that we know that a given string starts with a specific prefix/suffix, some specialized methods may be used.
So: Given the following, how do we remove the prefix and suffix?
std::string prefix = "prefix.";
std::string suffix = ".suffix";
std::string full_string = "prefix.content.suffix";
std::string just_the_middle = ???;
Since we know ahead of time that full_string starts/ends with prefix/suffix, one can use std::string::substr:
std::string prefix = "prefix.";
std::string suffix = ".suffix";
std::string full_string = "prefix.content.suffix";
// prefix removal
std::string result1 = full_string.substr(prefix.length());
// suffix removal
std::string result2 = full_string.substr(0, full_string.length() - suffix.length());
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