Connecting Tech Pros Worldwide Forums | Help | Site Map

Parsing a string

=?Utf-8?B?c2F0aA==?=
Guest
 
Posts: n/a
#1: Jul 17 '07
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.

Nathan Mates
Guest
 
Posts: n/a
#2: Jul 17 '07

re: Parsing a string


In article <E5792A82-F35F-4A86-8F67-CDB53D07E3AC@microsoft.com>,
=?Utf-8?B?c2F0aA==?= <sath@discussions.microsoft.comwrote:
Quote:
>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

--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
PvdG42
Guest
 
Posts: n/a
#3: Jul 17 '07

re: Parsing a string



"sath" <sath@discussions.microsoft.comwrote in message
news:E5792A82-F35F-4A86-8F67-CDB53D07E3AC@microsoft.com...
Quote:
>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.
You may want to look into the function strtok().

http://msdn2.microsoft.com/en-us/lib...sb(VS.80).aspx


Nathan Mates
Guest
 
Posts: n/a
#4: Jul 17 '07

re: Parsing a string


In article <eodQ9GByHHA.3940@TK2MSFTNGP05.phx.gbl>,
PvdG42 <pvdg@toadstool.eduwrote:
Quote:
>You may want to look into the function strtok().
>http://msdn2.microsoft.com/en-us/lib...sb(VS.80).aspx
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
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Ben Voigt [C++ MVP]
Guest
 
Posts: n/a
#5: Jul 17 '07

re: Parsing a string



"sath" <sath@discussions.microsoft.comwrote in message
news:E5792A82-F35F-4A86-8F67-CDB53D07E3AC@microsoft.com...
Quote:
>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
Quote:
>
Thanks in advance.

Hendrik Schober
Guest
 
Posts: n/a
#6: Jul 17 '07

re: Parsing a string


sath <sath@discussions.microsoft.comwrote:
Quote:
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.
Quote:
Thanks in advance.
HTH,

Schobi

--
SpamTrap@gmx.de 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


PvdG42
Guest
 
Posts: n/a
#7: Jul 17 '07

re: Parsing a string



"Nathan Mates" <nathan@visi.comwrote in message
news:139o86na3t7hlbe@corp.supernews.com...
Quote:
In article <eodQ9GByHHA.3940@TK2MSFTNGP05.phx.gbl>,
PvdG42 <pvdg@toadstool.eduwrote:
Quote:
>>You may want to look into the function strtok().
>>http://msdn2.microsoft.com/en-us/lib...sb(VS.80).aspx
>
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.

Closed Thread