472,353 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

evaluate boolean string?

Hi!
Does c# has any class method that will be able to evaluate
a given string e.g ((True && False) || (True && True)) and
return the result in boolean for this case is true

Thanks!
Nov 15 '05 #1
3 7906
Coco wrote:
Hi!
Does c# has any class method that will be able to evaluate
a given string e.g ((True && False) || (True && True)) and
return the result in boolean for this case is true

Thanks!


No. But, you can implement a very simple "interpreter" pattern with C#
and get the behavior. Search web for Dotnet and patterns.. you might see
someone already implemented this stuff.

--
Girish Bharadwaj

Nov 15 '05 #2
Thankf for the help, but i am not sure so sure how can it
be done, can u tell me more?
-----Original Message-----
Coco wrote:
Hi!
Does c# has any class method that will be able to evaluate a given string e.g ((True && False) || (True && True)) and return the result in boolean for this case is true

Thanks!
No. But, you can implement a very simple "interpreter"

pattern with C#and get the behavior. Search web for Dotnet and patterns.. you might seesomeone already implemented this stuff.

--
Girish Bharadwaj

.

Nov 15 '05 #3
"Coco" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
Hi!
Does c# has any class method that will be able to evaluate
a given string e.g ((True && False) || (True && True)) and
return the result in boolean for this case is true

Thanks!


Here's a simple class that can do it. It's not very efficient--for better
speed and memory use you'd want to use something else. Look up "infix
evaluation" for some more ideas. But this one is short and sweet and it's
fun to watch the expression cave in on itself each time through the loop.
You can easily extend it to include ==, != and other operators.

class BooleanEvaluator
{
public static bool Evaluate(string expression)
{
expression =
expression.ToLower(System.Globalization.CultureInf o.InvariantCulture);
expression = expression.Replace("false", "0");
expression = expression.Replace("true", "1");
expression = expression.Replace(" ", "");
string temp;
do
{
temp = expression;
expression = expression.Replace("(0)", "0");
expression = expression.Replace("(1)", "1");
expression = expression.Replace("0&&0", "0");
expression = expression.Replace("0&&1", "0");
expression = expression.Replace("1&&0", "0");
expression = expression.Replace("1&&1", "1");
expression = expression.Replace("0||0", "0");
expression = expression.Replace("0||1", "1");
expression = expression.Replace("1||0", "1");
expression = expression.Replace("1||1", "1");
}
while (temp != expression);
if (expression == "0")
return false;
if (expression == "1")
return true;
throw new ArgumentException("expression");
}
}

Nov 15 '05 #4

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

Similar topics

1
by: Bagger Vance | last post by:
The code below does not work -- but is there a way to do this? I want to both read a line and string sline; StreamReader m_streamReader =...
6
by: Michael G | last post by:
Hi, $PHONETIC is an array of strings. How can !$PHONETIC, in the case below, evalutae to true. It is a string not a boolean. It would make more...
22
by: Paminu | last post by:
As I remember if(1) evaluates to true and all other numbers including 0 evaluate to false. But where do I find out about this for sure?? I have...
13
by: TJS | last post by:
How can I evaluate this string in vb.net " Dim submenu0 As New skmMenu.MenuItem('Home', '') " I have tried this with skmmenu as a reference...
0
by: KenRoy | last post by:
I want to be able to evaluate a string value as either True or False. So if the string was "0 Or (1 or 0)" the value True would be returned. My...
3
by: willempie | last post by:
hi all, in FoxPro we know a function EVALUATE(xxxx) . This function evaluates the contents of xxxx. for example EVALUATE("X>10") will give...
4
by: Jim Florence | last post by:
Hello, I've just started in ASP and I'm having a few teething problems. Initially I tried to write out dates from the database using ...
2
kadghar
by: kadghar | last post by:
Many people asks if there is a way to write a mathematical expression, writen as a string in a text box, so they can do something like: sub...
2
by: yarborg | last post by:
This is kind of a weird one and hard to find answers online because of the format of the question. Essentially I want to be able to have a string...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.