Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

extract string

0 views
Skip to first unread message

Andrew Ostry

unread,
Jan 31, 2009, 8:24:35 PM1/31/09
to
how can i extract string between <rfid> and </rfid> using simple
string functions? (DO NOT USE XML parser)?

<cmd><rfid>ABCDEFG</rfid><user>Tom</user></cmd>

the string i need is ABCDEFG.

Ian Collins

unread,
Jan 31, 2009, 8:42:39 PM1/31/09
to

find "<rfid>", find "</rfid>" then get the sub-sting between them. See
the find() and substr() members of std::string.

--
Ian Collins

Sam

unread,
Jan 31, 2009, 9:17:06 PM1/31/09
to
Andrew Ostry writes:

If you need it, then use an XML parser. If you need to hammer in a nail,
using a screwdriver won't get you anywhere.


Erik Wikström

unread,
Feb 1, 2009, 6:34:40 AM2/1/09
to

But if you have a good screwdriver (one of the electric ones with a nice
and heavy battery) and you only need to get one small nail in then it is
unnecessary to go and buy a hammer.

Or put another way, if you need to parse XML you should get an XML
parser, but if all you need is to get a substring from an XML string
then you can just use the functionality provided by std::string. Adding
an XML parser might double (or more) your codebase and add additional
dependencies that have to be maintained.

--
Erik Wikström

Chris M. Thomasson

unread,
Feb 1, 2009, 8:57:18 AM2/1/09
to
"Andrew Ostry" <tobyc...@yahoo.com> wrote in message
news:v8u9o4d32n1anm8jo...@4ax.com...

_________________________________________________________________________
#include <stdio.h>
#include <string.h>


static int
display_sub_string(
char* src,
char const* str1,
char const* str2
) {
char* head = strstr(src, str1);
if (head) {
char* tail;
head += strlen(str1);
tail = strstr(head, str2);
if (tail) {
char tmp = tail[0];
tail[0] = '\0';
puts(head);
tail[0] = tmp;
return 0;
}
}
return -1;
}


int main(void) {
char str[] = "<cmd><rfid>ABCDEFG</rfid><user>Tom</user></cmd>";
display_sub_string(str, "<rfid>", "</rfid>");
return 0;
}
_________________________________________________________________________


Oh, I am sorry. Did you have to do your homework in C++? In that case, just
use `std::string'; no problem.

Lionel B

unread,
Feb 1, 2009, 10:57:10 AM2/1/09
to

I think, rather, that the appropriate cliche here may be: you don't need
a sledgehammer to crack a nut.

--
Lionel B

Sam

unread,
Feb 1, 2009, 7:26:27 PM2/1/09
to
Lionel B writes:

Correct. You need a nutcracker. It's a tool designed for the job, much like
an XML parser is a tool designed for the job of parsing XML content.


Rolf Magnus

unread,
Feb 12, 2009, 12:06:37 AM2/12/09
to
Sam wrote:

> Lionel B writes:
>
>> On Sat, 31 Jan 2009 20:17:06 -0600, Sam wrote:
>>
>>> Andrew Ostry writes:
>>>
>>>> how can i extract string between <rfid> and </rfid> using simple string
>>>> functions? (DO NOT USE XML parser)?
>>>>
>>>> <cmd><rfid>ABCDEFG</rfid><user>Tom</user></cmd>
>>>>
>>>> the string i need is ABCDEFG.
>>>
>>> If you need it, then use an XML parser. If you need to hammer in a nail,
>>> using a screwdriver won't get you anywhere.
>>
>> I think, rather, that the appropriate cliche here may be: you don't need
>> a sledgehammer to crack a nut.
>
> Correct.

No. Rather, I'd say the appropriate one is: Analogies are much like cracks
in the wall. Both are not useful in some situations.

> You need a nutcracker. It's a tool designed for the job,

And so it's ALWAYS the right tool? Even for someone who lives in the
outback, hundreds of miles from the next store without a car - and without a
nutcracker? What would you do in this situation? Go buy one or just use a
stone?

Sam

unread,
Feb 12, 2009, 7:00:04 AM2/12/09
to
Rolf Magnus writes:

> Sam wrote:
>
>
>> You need a nutcracker. It's a tool designed for the job,
>
> And so it's ALWAYS the right tool?

A nutcracker is always the right tool when the task is to crack nuts.

> Even for someone who lives in the
> outback, hundreds of miles from the next store without a car - and without a
> nutcracker?

Just because one isn't available, it doesn't make a nutcracker any less or
more appropriate for the job.

> What would you do in this situation? Go buy one or just use a
> stone?
>
>> much like an XML parser is a tool designed for the job of parsing XML
>> content.

However, we're not talking about nucrackers, but rather XML parsers. If one
was, for some reason, foisted with accomplishing this task on some oddball
platform for which no XML parser wasn't available, then perhaps
brute-forcing it might be justified.

But, however, XML parsers are aplenty on most platforms. You always have
them handy, they're always there, so if you need to parse some XML, that's
the right tool to use.

Of course, those who lack the mental agility to handle XML parsers and
insist on brute-forcing some hack job they invent on the spot -- there will
always be folks like that around. And, their contributions to the art of
computer science will always be a regular feature on www.thedailywtf.com


ZikO

unread,
Feb 12, 2009, 7:50:46 AM2/12/09
to
Ian Collins wrote:
> find "<rfid>", find "</rfid>" then get the sub-sting between them. See
> the find() and substr() members of std::string.
>

This!

... and here you can read about them:

http://www.cppreference.com/

Rolf Magnus

unread,
Feb 14, 2009, 10:16:30 AM2/14/09
to
Sam wrote:

> Rolf Magnus writes:
>
>> Sam wrote:
>>
>>
>>> You need a nutcracker. It's a tool designed for the job,
>>
>> And so it's ALWAYS the right tool?
>
> A nutcracker is always the right tool when the task is to crack nuts.
>
>> Even for someone who lives in the
>> outback, hundreds of miles from the next store without a car - and
>> without a nutcracker?
>
> Just because one isn't available, it doesn't make a nutcracker any less or
> more appropriate for the job.

Well, you can always turn around an "analogy" to fit your argument. Example:
The equivalent to "cracking the nut" here would be "finding the text
between <rfid> and </rfid>". For that, the nutcracker, i.e. simple text
processing is enough. I don't need an electric nutcracking machine that can
also peel fruits and boil coffee.

>> What would you do in this situation? Go buy one or just use a
>> stone?
>>
>>> much like an XML parser is a tool designed for the job of parsing XML
>>> content.
>
> However, we're not talking about nucrackers, but rather XML parsers.

That's my point.

> If one was, for some reason, foisted with accomplishing this task on some
> oddball platform for which no XML parser wasn't available, then perhaps
> brute-forcing it might be justified.
>
> But, however, XML parsers are aplenty on most platforms. You always have
> them handy, they're always there, so if you need to parse some XML, that's
> the right tool to use.

Or if the only thing you want is to find the part of the file that is between
<rfid> and </rfid> without adding a dependany on another library, without
adding full-fledged XML parsing for such a simple task and without having to
write more code just for interfacing with the XML parser.

0 new messages