472,370 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to evaluate a string expression to a boolean...

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 that looks like this "True AND True AND True" and evaluate that to a boolean which in this case would be True. Another example would be "(True or False) AND True" would be True. The reason for this is that I have a form where the user has to build some logic in a graphical interface. I can evaluate the logic and create a string but I don't know how to evaluate the string. The problem is that they can also choose items such as OR\AND\NOT which is hard to account for in code.

BTW I am working in VB .Net.


HELP!!
Jan 24 '08 #1
2 4901
I think I have this figured out. I borrowed some C# code that is similar and converted to VB .Net. I still don't completely understand why it works but I have verified that it does.

Private Function Eval(ByVal expression As String) As Boolean
expression = expression.ToLower
expression = expression.Replace("false", "0")
expression = expression.Replace("true", "1")
expression = expression.Replace(" ", "")
Dim temp As String

Do
temp = expression
expression = expression.Replace("(0)", "0")
expression = expression.Replace("(1)", "1")
expression = expression.Replace("0and0", "0")
expression = expression.Replace("0and1", "0")
expression = expression.Replace("1and0", "0")
expression = expression.Replace("1and1", "1")
expression = expression.Replace("0or0", "0")
expression = expression.Replace("0or1", "1")
expression = expression.Replace("1or0", "1")
expression = expression.Replace("1or1", "1")
Loop While temp <> expression

If expression = "0" Then
Return False
End If
If expression = "1" Then
Return True
End If
Throw New ArgumentException("expression")
End Function

Usage Example:
Eval("(True or True or False) and True")

Returns True as a boolean
Jan 24 '08 #2
Plater
7,872 Expert 4TB
Sounds like you are creating a regular grammer for yourself.
Seems like a workable enough solution.
Jan 24 '08 #3

Sign in to post your reply or Sign up for a free account.

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 = new StreamReader(fi); ...
1
by: David Laub | last post by:
I have no problems running the following dynamic XPath evaluator form MSXSL: <msxsl:script implements-prefix="dyn" language="jscript"> evaluate(context, expression) { return...
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 looked through K&R, all the C for dummies books...
3
by: Coco | last post by:
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!
13
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
8
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
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 True if X is greater then 10. is there a such...
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 something_click() textbox2.text=eval(textbox1.text)...
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
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 credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
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 server and have made sure to enable curl. I get a...
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. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...

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.