Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is my string::find() doing here

Tags:

c++

string

I have following code (simplified):

std::string file = "TestName.raw.craw";
int index1 = file.find_last_of(".craw");
int index2 = file.find_last_of(".raw");

Why is index1 == index2 after this? (both are 16)

Is the . skipped for some reason, or something? I checked at http://msdn.microsoft.com/en-us/library/h21280bw.aspx and the point was not in the list of characters that need to be escaped, so what is happening here?

like image 985
SinisterMJ Avatar asked Feb 23 '26 11:02

SinisterMJ


1 Answers

find_last_of() finds the last occurrence of any character that appears in its argument. Here, it's the final w in both cases.

You should use rfind() to find the last occurrence of a substring in any string.

like image 96
Mike Seymour Avatar answered Feb 24 '26 23:02

Mike Seymour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!