473,289 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Parsing string containing doubles

Hi,

I'm currently using this method to extract doubles from a string:

System::String* sp = S" ";
System::String* tokens[] = s->Trim()->Split(sp->ToCharArray());
m_Northing = System::Double::Parse(tokens[0], nfi);
m_Easting = System::Double::Parse(tokens[1], nfi);
m_Elevation = System::Double::Parse(tokens[2], nfi);

However, only strings that contain one " " are correctly parsed. How can I
do this for strings that contain arbitrary number of " " between doubles?
I.e.:
"79812.862 15532.942 1565.570" is ok, while
"79812.862 15532.942 1565.570" is not ok.

With C++ I would just use a stringstream. Can I do this with MC++ too? I
have not yet figure out how.

Thanks!

--
Daniel
Nov 22 '05 #1
7 1688
There is no real equivalent to C++'s scanf or iostreams in .NET. The thing
that comes closest to these are regular expressions (C# syntax):

using System.Text.RegularExpressions;
....
Match m = Regex.Match(yourString,
@"^\s*([0-9.,]+)\s+([0-9.,]+)\s+([0-9.,]+)\s*$");
if (m.Success)
{
Console.WriteLine(System.Double.Parse(m.Groups[1].ToString()));
Console.WriteLine(System.Double.Parse(m.Groups[2].ToString()));
Console.WriteLine(System.Double.Parse(m.Groups[3].ToString()));
}

However, Regex's are far more powerful, and it may take some time to get
used to them. It's worth the effort though, IMO.

Niki

"Daniel Lidström" <so*****@microsoft.com> wrote in
news:t2*****************************@40tude.net...
Hi,

I'm currently using this method to extract doubles from a string:

System::String* sp = S" ";
System::String* tokens[] = s->Trim()->Split(sp->ToCharArray());
m_Northing = System::Double::Parse(tokens[0], nfi);
m_Easting = System::Double::Parse(tokens[1], nfi);
m_Elevation = System::Double::Parse(tokens[2], nfi);

However, only strings that contain one " " are correctly parsed. How can I
do this for strings that contain arbitrary number of " " between doubles?
I.e.:
"79812.862 15532.942 1565.570" is ok, while
"79812.862 15532.942 1565.570" is not ok.

With C++ I would just use a stringstream. Can I do this with MC++ too? I
have not yet figure out how.

Thanks!

--
Daniel

Nov 22 '05 #2
Have you considered using replace to clean your string before you call
split? You could replace the " " with " ". Just a thought then you
would have to change your other code. It only requires one extra line
per type of clean up you need to do.

Wiz

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 22 '05 #3
What if the input is separated by 3 spaces?
....or 4?

Niki

"WizyDig" <so*****@microsoft.com> wrote in
news:uI****************@TK2MSFTNGP10.phx.gbl...
Have you considered using replace to clean your string before you call
split? You could replace the " " with " ". Just a thought then you
would have to change your other code. It only requires one extra line
per type of clean up you need to do.

Wiz

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 22 '05 #4
On Wed, 18 Aug 2004 15:44:26 +0200, Niki Estner wrote:
There is no real equivalent to C++'s scanf or iostreams in .NET. The thing
that comes closest to these are regular expressions (C# syntax):

using System.Text.RegularExpressions;
...
Match m = Regex.Match(yourString,
@"^\s*([0-9.,]+)\s+([0-9.,]+)\s+([0-9.,]+)\s*$");
if (m.Success)
{
Console.WriteLine(System.Double.Parse(m.Groups[1].ToString()));
Console.WriteLine(System.Double.Parse(m.Groups[2].ToString()));
Console.WriteLine(System.Double.Parse(m.Groups[3].ToString()));
}

However, Regex's are far more powerful, and it may take some time to get
used to them. It's worth the effort though, IMO.


How would I write this in MC++? I tried this but got compilation errors:

#using <System.dll>

using namespace System::Text::RegularExpressions;
....
Match* m = Regex::Match(s,
"^\\s*([0-9.,]+)\\s+([0-9.,]+)\\s+([0-9.,]+)\\s*$");
if( m->Success ){
m_Northing = System::Double::Parse(m->Groups->Item(1)->ToString());
m_Easting = System::Double::Parse(m->Groups->Item(2)->ToString());
m_Elevation = System::Double::Parse(m->Groups->Item(3)->ToString());
}

(113) : error C2064: term does not evaluate to a function taking 1
arguments
(113) : error C2227: left of '->ToString' must point to class/struct/union

113 refers to m_Northing = ... line. Also, is the regular expression
written to be equivalent with the CS example? What does the @ at the start
of the CS RE mean?
Thanks!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #5
"Daniel Lidström" <so*****@microsoft.com> wrote in
news:se****************************@40tude.net...
...
How would I write this in MC++? I tried this but got compilation errors:
...
Item is an indexer; Try:
m_Northing = System::Double::Parse(m->Groups->Item[1]->ToString());
m_Easting = System::Double::Parse(m->Groups->Item[2]->ToString());
m_Elevation = System::Double::Parse(m->Groups->Item[3]->ToString());
Also, is the regular expression
written to be equivalent with the CS example?
Yes, I think so.
What does the @ at the start of the CS RE mean?


In a "normal" C# string you have to escape backslashes like in C. However if
you prefix the string literal with @, you can put in backslashes like any
other character. Nice feature for paths or regular expressions.
Maybe you should have a look at a tool like Expresso
(http://www12.brinkster.com/ultrapico/Expresso.htm). It's perfect for
testing a regular expression on sample data, and it can create correct MC++
code (with correctly escaped backslashes).

Just curious: why do you use MC++? I tend to use C# where I can, because I
don't like all those special keywords like "__gc", "__value" and so on.

Niki
Nov 22 '05 #6
On Thu, 19 Aug 2004 12:21:41 +0200, Niki Estner wrote:
Maybe you should have a look at a tool like Expresso
(http://www12.brinkster.com/ultrapico/Expresso.htm). It's perfect for
testing a regular expression on sample data, and it can create correct MC++
code (with correctly escaped backslashes).


That's a great tool! Many thanks for pointing me to it.

--
Daniel
Nov 22 '05 #7
This small chunk of code will do what you are looking for Niki.
string t = " some string with odd number of spaces";
int i = t.IndexOf(" ",0);
int j = t.IndexOf(" ",0);
while( i > 0 || j > 0)
{
t = t.Replace(" "," ");
i = t.IndexOf(" ",0);
j = t.IndexOf(" ",0);
}

Hope that helps

-Wiz

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
16
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I only know a little bit of xml and I'm trying to parse a xml document in order to save its elements in a file (dictionaries inside a list)....
7
by: Daniel Lidström | last post by:
Hi, I'm currently using this method to extract doubles from a string: System::String* sp = S" "; System::String* tokens = s->Trim()->Split(sp->ToCharArray()); m_Northing =...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
4
by: Ron | last post by:
Hi, I need to parse text (ie. created in Notepad) files for numbers (doubles). In Borland C++ Builder the following works: if(!InVect.is_open()) { InVect.open(TxtFileName.c_str()) ; }
26
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What...
3
gagandeepgupta16
by: gagandeepgupta16 | last post by:
I am having problem in parsing a string containing HTML Tags. The situation is somewhat similar to the following as quoted in some other forum : if (typeof DOMParser == "undefined") { ...
31
by: broli | last post by:
I need to parse a file which has about 2000 lines and I'm getting told that reading the file in ascii would be a slower way to do it and so i need to resort to binary by reading it in large...
28
by: pereges | last post by:
Hi I've a string input and I have to parse it in such a way that that there can be only white space till a digit is reached and once a digit is reached, there can be only digits or white space till...
6
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.