471,337 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Evaluating numeric expressions

Is there an easy way in C# to take a string that contains an expression, say
for example something like '(10 / 2) + 1' and evaluate it without having to
parse the string myself and muck about with other stuff like operator
precedence?
Jun 27 '08 #1
4 1742
Maybe you can try with RegularExpressions

string local_pattern = @"[^0-9]";
System.Text.RegularExpressions.Regex local_RegEx = new
System.Text.RegularExpressions.Regex(local_pattern );
return local_RegEx.Replace(local_InputString, "");

"PatrickS" wrote:
Is there an easy way in C# to take a string that contains an expression, say
for example something like '(10 / 2) + 1' and evaluate it without having to
parse the string myself and muck about with other stuff like operator
precedence?
Jun 27 '08 #2
isn't that going to return 1021? what kind of evaluation is that?

"Frank Uray" wrote:
Maybe you can try with RegularExpressions

string local_pattern = @"[^0-9]";
System.Text.RegularExpressions.Regex local_RegEx = new
System.Text.RegularExpressions.Regex(local_pattern );
return local_RegEx.Replace(local_InputString, "");

"PatrickS" wrote:
Is there an easy way in C# to take a string that contains an expression, say
for example something like '(10 / 2) + 1' and evaluate it without having to
parse the string myself and muck about with other stuff like operator
precedence?
Jun 27 '08 #3
PatrickS wrote:
Is there an easy way in C# to take a string that contains an
expression, say for example something like '(10 / 2) + 1' and
evaluate it without having to parse the string myself and muck about
with other stuff like operator precedence?
You can invoke the C# compiler to create a class with a static method, then
call the method. Doing this without memory leaks is not easy and very bad
performance.

I would look into JScript.NET which has an eval function.
Jun 27 '08 #4
PatrickS wrote:
Is there an easy way in C# to take a string that contains an expression, say
for example something like '(10 / 2) + 1' and evaluate it without having to
parse the string myself and muck about with other stuff like operator
precedence?
JavaScript has a nice eval function that can be utilized from C#.

See http://www.vajhoej.dk/arne/eksperten...7_08/evaljs.cs for
a code example.

Arne
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Jason Heyes | last post: by
10 posts views Thread by John A Grandy | last post: by
1 post views Thread by kaosyeti | last post: by
10 posts views Thread by cybernerdsx2 | last post: by
4 posts views Thread by pinkfloydhomer | last post: by
6 posts views Thread by Mark Chimes | last post: by
7 posts views Thread by david | last post: by
reply views Thread by rosydwin | last post: by

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.