473,385 Members | 1,919 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,385 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 5040
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.