stl:string

Can anyone tell me how can I implement the find algorithm on a string to search from a beginning postion to a defined position for a specific hex/char
Thx
veselAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

joshaubryCommented:
just create a substring that starts at the beginning of your string and goes to definedPosition, then find what you are looking for and the result will be its position in your original string as well

string sub = yourString.substr(0, definedPosition);
int n = sub.find(yourSearchChar);

here, n is the position in yourString of yourSearchChar
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
You can do that directly with basic_string::find
size_type find(const basic_string& s, size_type pos = 0) const

So like in
char searchfor;
string lookin;
int fromhere;
int foundat = lookin.find(searchfor, fromhere);
0
joshaubryCommented:
but in your example he wants fromhere to be the ending place not the starting point of the search, he wants to go from the beginning to fromhere, not fromhere to the end....
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

KangaRooCommented:
I see, well actually it read like both, beginning and ending can be variable:
>> from a beginning postion to a defined position
Anyway, string has no member to do so directly.
0
joshaubryCommented:
well, if he wants to start from a certain beginning position, he just has to modify my code from

string sub = yourString.substr(0, definedPosition);
int n = sub.find(yourSearchChar);

to

string sub = yourString.substr(beginningPosition, definedPosition);
int n = sub.find(yourSearchChar);
0
jasonclarkeCommented:
string is a sort of STL container, so you should be able to use the standard STL find algorithm:

char* posn = std::find(&string[beginPos], &string[endPos], searchChar);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.