[Solved] how to remove a substring of unknown length from a string – C++ [closed]


Use rfind to get the location of the last slash then substr to return the right side of the string. Since substr‘s second argument defaults to “the end of the string” you only need to provide the beginning position:

#include <string>

std::string GetURLFilePath(const std::string& url) {
    auto last_slash_location = url.rfind("https://stackoverflow.com/");
    if(last_slash_location == std::string::npos || last_slash_location + 1 >= url.size()) {
        return "";
    }
    return url.substr(last_slash_location + 1);
}

You didn’t really say if you are expecting parameters (https://www.youtube.com/watch?v=YnWhqhNdYyk) so this will obviously not be exactly what you want if those exist in the string, but it’ll get you to the right track.

1

solved how to remove a substring of unknown length from a string – C++ [closed]