str.find(str1) usage ?

K

KL

Hello again,

I am still working on this homework assignment and have hit a wall.

I have a list that I want to fill with all occurences of img tags from a
big string of html code. So I have a string called str which holds the
html code and I want to find the string str1 which would be <img and
then write to the list an item that contains all the chars from <img
through the > that closes the img tag.

I was thinking that I would use str.find(str1) in this case, but I am
not completely sure of the syntax, and how to apply it to this
situation. I don't have any code to post, because I really am stuck at
the beginning of this part.

Just a reminder, this is a homework assignment, so I would prefer
answers that help point me in the right direction rather than just the
code to use.

Thanks again
 
V

Victor Bazarov

KL said:
I have a list that I want to fill with all occurences of img tags
from a big string of html code. So I have a string called str which
holds the html code and I want to find the string str1 which would be
<img and then write to the list an item that contains all the chars
from <img through the > that closes the img tag.

Are you sure you're not going to find those inside, say, comments?
I was thinking that I would use str.find(str1) in this case, but I am
not completely sure of the syntax, and how to apply it to this
situation. I don't have any code to post, because I really am stuck
at the beginning of this part.

What you're asking is not a simple thing. In many cases the html text
is a hierarchical data set, which needs to be put in a special form to
be understood. For example, if I write "<img" inside a "<pre>" segment,
finding it is not going to do any good, is it?

Of course, for a simple page you can assume that there are no nested
tags. In that case RTFM about 'find', it returns an index, IIRC. You
need to check if it's equal to 'std::string::npos', which means the
string was not found. Also pay attention that there are several 'find'
variations. You can supply the starting position from which to search.
For example, to find a matching pair of two strings you find the first
using 'find', and then find the second _starting_ from the position of
the first one (if it has been found, of course).
Just a reminder, this is a homework assignment, so I would prefer
answers that help point me in the right direction rather than just the
code to use.

BTW, what book are you reading that doesn't give examples of using the
'std::string' class? I strongly recommend "The C++ Standard Library"
by Josuttis.

V
 
A

Aleksander Beluga

KL said:
Hello again,
I was thinking that I would use str.find(str1) in this case, but I am
not completely sure of the syntax, and how to apply it to this
situation. I don't have any code to post, because I really am stuck at
the beginning of this part.

What syntax are unsure of? Syntax of find or syntax of your string?
If former, read out the STL documentatation.
Just a reminder, this is a homework assignment, so I would prefer
answers that help point me in the right direction rather than just the
code to use.
Try using substr.
Thanks again
No problem.
 
K

KL

Are you sure you're not going to find those inside, say, comments?

It really won't matter for this particular assignment. I have already
taken the html code and saved it into one long string.
What you're asking is not a simple thing. In many cases the html text
is a hierarchical data set, which needs to be put in a special form to
be understood. For example, if I write "<img" inside a "<pre>" segment,
finding it is not going to do any good, is it?

We just need to creat a STL container that holds all the img tags. So
anything that starts said:
Of course, for a simple page you can assume that there are no nested
tags. In that case RTFM about 'find', it returns an index, IIRC. You
need to check if it's equal to 'std::string::npos', which means the
string was not found. Also pay attention that there are several 'find'
variations. You can supply the starting position from which to search.
For example, to find a matching pair of two strings you find the first
using 'find', and then find the second _starting_ from the position of
the first one (if it has been found, of course).

So if my thinking is right (and that isn't always the case),
str.find(str1) will return the position where the first case of str1
occurs in str. I could then use a while loop to write each char into
another_string until I find the closing > tag. Then
push_back(another_string) into my list.

Now I am not clear on how to continue doing a find for the successive
cases of str1. Perhaps you could clarify that a bit more for me?
BTW, what book are you reading that doesn't give examples of using the
'std::string' class? I strongly recommend "The C++ Standard Library"
by Josuttis.

The book I am using is Walter Savitch's Problem Solving with C++. I
find it to be quite helpful, and it does have examples of using the
'std::string' class, I just am a bit confused in exactly how to apply
what it says and demonstrates.
 
T

TB

KL skrev:
So if my thinking is right (and that isn't always the case),
str.find(str1) will return the position where the first case of str1
occurs in str. I could then use a while loop to write each char into
another_string until I find the closing > tag. Then
push_back(another_string) into my list.

Now I am not clear on how to continue doing a find for the successive
cases of str1. Perhaps you could clarify that a bit more for me?

There are several overloaded alternatives of find(), of which one is:

find (const basic_string& str, size_type pos = 0) const;

Searches for the first occurrence of the substring specified by str in
this string, starting at position pos. If found, it returns the index
of the first character of the matching substring. If not found, returns
npos. Equality is defined by traits::eq().

<snip>
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top