-2

I'm using WDL_String container to manage string within a C++ Framework. I need to split a string (delimiter \) and get the last occurrence.

I can use the native method:

mFilePath.DeleteSub(mLastOccPosition, mFilePath.GetLength());

subbing the string, but I don't know how to catch the mLastOccPosition.

So if I have this string:

D:\\Google Drive\\My Files\\Test.zip

I need to return Test.zip. What's the best way to do this in C++?

markzzz
  • 44,504
  • 107
  • 265
  • 458

1 Answers1

1

If s is an instance of std::string, then s.find_last_of('\\') would do it.

It returns std::string::npos if it can't be found.

You can then use std::string::substr to extract the bit you need.

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
  • As I said, I'm not working with string. But WDL_String. I can get `char*` from WDL_String. – markzzz Jun 10 '16 at 08:49
  • Well, you did ask for the best way, and that's probably to write `std::string s(/*the function that gets the char* */)`, and go from there. There's no point in building the extraction code from scratch. – Bathsheba Jun 10 '16 at 08:50
  • Is it nice somethings like this? (I don't need any reference to string): `(string(mFilePath.Get())).find_last_of('\\')` – markzzz Jun 10 '16 at 09:08
  • That would do it. (Although you might want to keep the string around if you want to use it for the extraction of the file name). Note that `std::string::npos` is a *constant* and does not require a string instance. – Bathsheba Jun 10 '16 at 09:10