0

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";
3
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by Jennifer84
0

There are many ways but here's one:

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

I didn't test this BTW.

0

EDITED: Added the extra \

That could even be:

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

to make it a bit shorter.

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.