none
How to conver string to bool RRS feed

  • Question

  • For example I have a string "a == b && a != c" and I want  to use it in if condition how do I convert it in to bool
    Tuesday, May 7, 2019 6:59 PM

Answers

All replies

  • Seems you could use Char.Equals.

    using System;
    
    public class EqualsSample {
    	public static void Main() {
    		char chA = 'A';
    		char chB = 'B';
    
    		Console.WriteLine(chA.Equals('A'));		// Output: "True"
    		Console.WriteLine('b'.Equals(chB));		// Output: "False"
    	}
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 7, 2019 7:36 PM
    Moderator
  • If you are referring to being able to take an arbitrary string and evaluate it at runtime then this is a non-trivial problem to solve. Basically you have to build an expression parser. There are quite a few around which you can google for but they all have the same basic behavior. The challenge is getting the actual variables (a, b, c) into the context of the expression so it can evaluate.

    In your very specific case where you're using C# then you could rely on the old ASP.NET Binder class which used to do this but it was more generic. Alternatively you can use Roslyn to effectively convert your expression to C# code. However you'll still need to figure out how to identify what "variables" the code is using so you can inject the values. For a simple article on parsing expressions with Roslyn take a look at this older article. Note that this is going to be expensive so it isn't something you'll likely want to do often. If you need this kind of functionality it might be easier to simplify your expression support and use a simple expression parser available online. Still going to be slow but probably not as slow as Roslyn would be given what all is supported.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, May 7, 2019 8:02 PM
    Moderator
  • For example I have a string "a == b && a != c" and I want  to use it in if condition how do I convert it in to bool

    Hi BataBo Jokviu,

    Thank you for posting here.

    The solution of your question depends on what ‘a’, ‘b’ and ‘c’ stands for.

    If they are “char” type, as Karen Payne suggested, you can use “Char.Equals”:

    if ('a'.Equals('b') && !('a'.Equals('c')))
    {
                  // code
    }
    

    However, if they are the custom data type, you need to override Equals function and GetHashCode function.

    Hope this helps:
    https://www.codeproject.com/Tips/1255596/Overriding-Equals-GetHashCode-Laconically-in-CShar


    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 8, 2019 6:12 AM
    Moderator
  • Or if you just want limited functionality for homework, I suggest you to tokenize the string, create evaluation parser that pushes operand into Stack<string> according to your defined evaluation order, then prepare a big "switch" block to do the operations like storing value into hashtable, load them and perform varies operations.
    Wednesday, May 8, 2019 6:24 AM
    Answerer
  • At the end pf everything I made a program that write python script and than executes it
    Sunday, May 12, 2019 3:04 PM