468,257 Members | 1,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

str.find(str1) usage ?

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

--
KL
Mar 4 '06 #1
4 4357
KL wrote:
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
--
Please remove capital As from my address when replying by mail
Mar 4 '06 #2
KL wrote:
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.
Mar 4 '06 #3
KL
on 3/4/2006 10:27 AM Victor Bazarov said the following:
KL wrote:
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?


It really won't matter for this particular assignment. I have already
taken the html code and saved it into one long string.
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?


We just need to creat a STL container that holds all the img tags. So
anything that starts <img through the closing >
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?
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.


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.

--
KL
V

Mar 4 '06 #4
TB
KL skrev:
on 3/4/2006 10:27 AM Victor Bazarov said the following:
KL wrote:
<snip>
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>
--
TB @ SWEDEN
Mar 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Hansen | last post: by
8 posts views Thread by rbt | last post: by
5 posts views Thread by Nils O. Selåsdal | last post: by
11 posts views Thread by Michael Moreno | last post: by
10 posts views Thread by rdemyan via AccessMonster.com | last post: by
1 post views Thread by Tim Kelley | last post: by
2 posts views Thread by jld | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.