Some simple ExpressionEvaluator examples

CSharp · Console

/Program.cs

/**********************************************************************************************
* This fiddle is a demo of the library : CodingSeb.ExpressionEvaluator
* See : https://github.com/codingseb/ExpressionEvaluator
* For Others examples : https://dotnetfiddle.net/Packages/41132/CodingSeb_ExpressionEvaluator
**********************************************************************************************/

using System;
using System.Collections.Generic;
using CodingSeb.ExpressionEvaluator;
					
public class Program
{
	public static void Main()
	{
		ExpressionEvaluator evaluator = new ExpressionEvaluator();
		
		List<string> expressions = new List<string>()
		{
			"1+1",
			"2 + 3 * 2",
			"(2 + 3) * 2",
			"Pi",
			"Pow(2, 4)",
			"Sqrt(2) / 3",
			"\"Hello\" + \" \" + \"World\"",
			"Max(1+1, 2+3, 2*6, Pow(2, 3))",
			"Array(2, $\"Test { 2 + 2 } U\", true).Length",
			"Array(2, $\"Test { 2 + 2 } U\", true)[1]",
			"Array(2, $\"Test { 2 + 2 } U\", true)[2] ? \"yes\" : \"no\"",
			"false ? \"yes\" : \"no\"",
			"\"Hello\\nWorld\"",
			"@\"Hello\\nWorld\"",
			"Regex.Match(\"Test 34 Hello/-World\", @\"\\d+\").Value",
			"int.Parse(Regex.Match(\"Test 34 Hello/-World\", @\"\\d+\").Value) + 2",
			"3 / 2",
			"3d / 2d",
			"(float)3 / (float)2",
			"new(Random).Next(1,10)",
			"new Regex(@\"\\w*[n]\\w*\").Match(\"Which word contains the desired letter ?\").Value",
			"List(\"Test\", \"Hello\", \"Bye\", \"How are you?\").Find(t => t.Length < 4)",
			"new List<string>(){ \"Test\", \"Hello\", \"Bye\", \"How are you?\" }.Find(t => t.Length < 4)",
			"Enumerable.Range(1,4).Cast().Sum(x =>(int)x)",
			"Enumerable.Repeat(3,6).Cast().ToList().Count",
			"Enumerable.Repeat(3,6).Cast().ToList()[4]",
			"((x, y) => x * y)(4, 2)",
			"\"Hello\"[2] == 'l'",
			"\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ',')).Length",
			"\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ','))[0]",
			"\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ','))[1]",
			"\"Hello,Test,What\".Split(ArrayOfType(typeof(char), ','))[2]",
			"\"Hello,Test,What\".Split(new char[] {','}).Length",
			"\"Hello,Test,What\".Split(new char[] {','})[0]",
			"\"Hello,Test,What\".Split(new char[] {','})[1]",
			"\"Hello,Test,What\".Split(new char[] {','})[2]",
			"new System.Xml.XmlDocument().FluidLoadXml(\"<root><element id='MyElement'>Xml Content</element></root>\").SelectSingleNode(\"//element[@id='MyElement']\").InnerXml",
			"$\"https://www.google.ch/search?q={System.Net.WebUtility.UrlEncode(\"request with url encode() ?\")}\""
		};
		
		expressions.ForEach(expression => 
		{
			Console.WriteLine(expression);
			Console.WriteLine(evaluator.Evaluate(expression));
			Console.WriteLine(string.Empty);
		});
	}
}
;