How it works...

Before we look at how the new string type works, let's consider the following example of a function that is supposed to extract the name of a file without its extension. This is basically how you would write the function from the previous section before C++17.

Note that in this example the file separator is \ (backslash) as in Windows. For Linux-based systems, it has to be changed to / (slash).
    std::string get_filename(std::string const & str)     {       auto const pos1 {str.find_last_of('')};       auto const pos2 {str.find_last_of('.')};       return str.substr(pos1 + 1, pos2 - pos1 - 1);     }     auto name1 = get_filename(R"(c:\test\example1.doc)"); // example1     auto name2 = get_filename(R"(c:\test\example2)");     // example2  if(get_filename(R"(c:\test\_sample_.tmp)").front() ...

Get Modern C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.