473,378 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

string find last of problem

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;

Apr 4 '07 #1
3 5764
ka*****@hotmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 4 '07 #2
On Apr 5, 6:15 am, "kafe...@hotmail.com" <kafe...@hotmail.comwrote:
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/

Apr 5 '07 #3
use find for exact pattern to be searched.
Sarath wrote:
On Apr 5, 6:15 am, "kafe...@hotmail.com" <kafe...@hotmail.comwrote:
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/
Apr 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris Mantoulidis | last post by:
PROBLEM: I'm having some weird problems with string::find() (in ParamGenerate()), but since I'm not sure if that is the source of all bad output in my program, I'm posting least code that's...
3
by: Chris Mantoulidis | last post by:
I posted this here one day ago but it seems like it hasn't been put up for some unknown reason. That gives me a chance to say things a bit better in this post. 1st of all let's desribe the...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
5
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
12
by: Ron | last post by:
I am getting an error Option strict on disallows implicit conversion from string to long I get it for this code iStartPosition = InStr(iStartPosition + 1, RichTextBox1.Text, "A" Or "a" the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.