This post will discuss how to remove the prefix from a string in C++.

The standard solution to remove part of a string is using the std::string::erase member function. It can be used as follows:

Download  Run Code

 
To get the copy of the string with the prefix removed, we can use the std::string::substr function:

Download  Run Code

 
It is advisable to check whether a string starts with a prefix before removing it. This can be achieved using C++20 std::string.starts_with() or std::string::substr function.

Download Code

That’s all about removing the prefix from a string in C++.