I have 2 strings like this:
In these 2 cases I want to get "File1.txt" and "File2.txt" and put them to a string.
I need the same method so I suppose I have to find the Last "/" in the string and then
put the rest to a new string.

What method and approach can be used to do this ?

std::string Line1 = "c:\Folder1\File1.txt";
std::string Line2 = "c:\Folder1\Folder2\File2.txt";

Recommended Answers

All 4 Replies

There are many ways but here's one:

string filename = Line1.substr(Line1.rfind("\\")+1, string::npos);

I didn't test this BTW.

EDITED: Added the extra \

That could even be:

string filename = Line1.substr(Line1.rfind("\\")+1);

to make it a bit shorter.

Thanks again codeaa. That solved the problem nicely !

EDITED: Added the extra \

That could even be:

string filename = Line1.substr(Line1.rfind("\\")+1);

to make it a bit shorter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.