Parsing a string

G

Guest

I need to parse a string which is like
ACS*PR*1*101.55**2*-22**66*166*68~
The data between the asterisks should go to sepate strings (string1, string2
etc)
Can I string instead of c-string?
Any help is appreciated.

Thanks in advance.
 
N

Nathan Mates

I need to parse a string which is like
ACS*PR*1*101.55**2*-22**66*166*68~
The data between the asterisks should go to sepate strings (string1, string2
etc)

If you're matching things between single '*'s, then strchr is your
friend for finding the next instance of a character in a
string. Fairly easy to make a loop that calls strchr and strdup if
you're doing c-strings.

Nathan Mates
 
N

Nathan Mates


Why are people pushing that braindamaged bit of code? strtok is
*dangerous*. Library functions that have globals behind your back are
a recipe for disaster. It's only slightly better than gets(). See
things like: http://xp-framework.info/xml/xp.en_US/news/view?31

Sure, MS might have fixed some of the evil with strtok_s. If so,
then recommend it, and only it. But, as the original poster is using
single characters to separate things, then strchr/strrchr will do the
trick, and is *SAFE*.

Nathan Mates
 
B

Ben Voigt [C++ MVP]

sath said:
I need to parse a string which is like
ACS*PR*1*101.55**2*-22**66*166*68~
The data between the asterisks should go to sepate strings (string1,
string2
etc)
Can I string instead of c-string?
Any help is appreciated.

Since you're posting in the C++/CLI group, try System::String::Split
 
H

Hendrik Schober

sath said:
I need to parse a string which is like
ACS*PR*1*101.55**2*-22**66*166*68~
The data between the asterisks should go to sepate strings (string1, string2
etc)
Can I string instead of c-string?
Any help is appreciated.

If speed doesn't matter, you can put it into a
stringstream and read with 'std::getline()' using '*'
as the EOL char until you hit EDF. Or look into the
'std::string::find...()' family of functions and use
one of these together with 'std::string::substr()'.
Both ways avoid C-strings.
Thanks in advance.

HTH,

Schobi

--
(e-mail address removed) is never read
I'm HSchober at gmx dot de
"If there were some arcane way to remove the heads of every
newsgroup troll on the planet, I think it would elevate
humans to a whole new level of intelligence."
Rocky Frisco
 
P

PvdG42

Nathan Mates said:
Why are people pushing that braindamaged bit of code? strtok is
*dangerous*. Library functions that have globals behind your back are
a recipe for disaster. It's only slightly better than gets(). See
things like: http://xp-framework.info/xml/xp.en_US/news/view?31

Sure, MS might have fixed some of the evil with strtok_s. If so,
then recommend it, and only it. But, as the original poster is using
single characters to separate things, then strchr/strrchr will do the
trick, and is *SAFE*.

Nathan Mates
--
While I'm sure you have a point, why the reference to this "XP Framework",
which appears to be some sort of class library for PHP? Links from the cited
article all lead to PHP.
 

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

Top