String Manipulation doubt

N

noopathan

I have two strings as below

string A = "102030405060.wldep.1251.jpg";
string B= "102030405060.wldep.1251.pdf";

I want the word after last dot in other two strings
i.e;
string C = "jpg";
string D="pdf";

Early code help is greatly appreciated.

regards

Jango
 
B

Ben Pope

noopathan said:
I have two strings as below

string A = "102030405060.wldep.1251.jpg";
string B= "102030405060.wldep.1251.pdf";

I want the word after last dot in other two strings
i.e;
string C = "jpg";
string D="pdf";

Try

std::find_last_of

Ben Pope
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

noopathan said:
I have two strings as below

string A = "102030405060.wldep.1251.jpg";
string B= "102030405060.wldep.1251.pdf";

I want the word after last dot in other two strings
i.e;
string C = "jpg";
string D="pdf";

Early code help is greatly appreciated.

regards

Jango

Use std::string::rfind.

Regards, Stephan
 
D

Daniel T.

"noopathan said:
I have two strings as below

string A = "102030405060.wldep.1251.jpg";
string B= "102030405060.wldep.1251.pdf";

I want the word after last dot in other two strings
i.e;
string C = "jpg";
string D="pdf";

Early code help is greatly appreciated.

regards

Jango

assert( A.rfind('.') != string::npos );
string C = A.substr( A.rfind('.') + 1 );
 
J

Jim Langston

Daniel T. said:
assert( A.rfind('.') != string::npos );
string C = A.substr( A.rfind('.') + 1 );

Don't use assert for something like this you want to put into production.
Once you turn off debug mode asserts disappear. So it would totally ignore
that. I would rather do:

std::string C;
if ( A.rfind('.') != string::npos )
C = A.substr( A.rfind('.') + 1 );

That'll work in production or debug. Yes, it assigns a null string and
doesn't bring up an error box, but you can MessageBox if you want manually.
 
D

Daniel T.

"Jim Langston said:
Don't use assert for something like this you want to put into production.
Once you turn off debug mode asserts disappear. So it would totally ignore
that. I would rather do:

std::string C;
if ( A.rfind('.') != string::npos )
C = A.substr( A.rfind('.') + 1 );

That'll work in production or debug. Yes, it assigns a null string and
doesn't bring up an error box, but you can MessageBox if you want manually.

It all depends on what you want to accomplish. Your solution is good if
the program should expect to occasionally get a string without a '.' in
it (for example if the user is entering the string.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top