string find last of problem

K

kaferro

I need to find "DEL" in a string. Sometimes it will appear at the
beginning sometimes at the end. I test for its existence if pos is
greater than zero when using the string function find_last_of(). This
method worked fine until I encountered an unanticipated string value.
What is the best method to solve the problem that exists in the code
below where both find_last_of() return a positive value?

//determines if trade type is DEL
string s_transStatus="DEL";
int pos=0;
pos=s_transStatus.find_last_of("DEL");

cout << "\ns_transStatus=" << s_transStatus;
cout << "\npos=" << pos << endl;

s_transStatus="ELEC";
pos=0;
pos=s_transStatus.find_last_of("DEL");

cout << "\ns_transStatus=" << s_transStatus;
cout << "\npos=" << pos << endl;
 
V

Victor Bazarov

I need to find "DEL" in a string.

You need to find an exact substring?
Sometimes it will appear at the
beginning sometimes at the end. I test for its existence if pos is
greater than zero when using the string function find_last_of().

Wrong function, if I understand what you're trying to do. 'find_last_of'
finds the last occurrence of _any_ of the chars from { 'D', 'E', 'L' } in
your string.
This
method worked fine until I encountered an unanticipated string value.

Like what?
What is the best method to solve the problem that exists in the code
below where both find_last_of() return a positive value?

Not sure what you're asking. What exactly are you trying to accomplish
here?
//determines if trade type is DEL
string s_transStatus="DEL";
int pos=0;
pos=s_transStatus.find_last_of("DEL");

You can write it in the same line:

int pos = s_transStatus.find_last_of("DEL");

I believe you still need to simply use 'rfind' instead of 'find_last_of'.
cout << "\ns_transStatus=" << s_transStatus;
cout << "\npos=" << pos << endl;

s_transStatus="ELEC";
pos=0;
pos=s_transStatus.find_last_of("DEL");

cout << "\ns_transStatus=" << s_transStatus;
cout << "\npos=" << pos << endl;

V
 
S

Sarath

I need to find "DEL" in a string. Sometimes it will appear at the
beginning sometimes at the end. I test for its existence if pos is
greater than zero when using the string function find_last_of(). This
method worked fine until I encountered an unanticipated string value.
What is the best method to solve the problem that exists in the code
below where both find_last_of() return a positive value?

//determines if trade type is DEL
string s_transStatus="DEL";
int pos=0;
pos=s_transStatus.find_last_of("DEL");

cout << "\ns_transStatus=" << s_transStatus;
cout << "\npos=" << pos << endl;

s_transStatus="ELEC";
pos=0;
pos=s_transStatus.find_last_of("DEL");

cout << "\ns_transStatus=" << s_transStatus;
cout << "\npos=" << pos << endl;

As per the documentation, find_last_of searches through a string for
the last character that matches any element of a specified string.
Seems the program behavior is correct. I suggest you to use find or
rfind function for your purpose.

-Sarath
http://sarathc.wordpress.com/
 
N

nishit.gupta

use find for exact pattern to be searched.
Sarath said:
As per the documentation, find_last_of searches through a string for
the last character that matches any element of a specified string.
Seems the program behavior is correct. I suggest you to use find or
rfind function for your purpose.

-Sarath
http://sarathc.wordpress.com/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top