1

I have strings like following:

"[Current.Age] - 10"
"[Current.Height] + 50"
"[Current.Age] + 10 - [Current.Height] - 50"

And I'd like to replace [Current.Something] with the numerical value of the current selected object, for example a selected object may have the following state:

var student = new Student();
student.Age = 20;
student.Height = 180;

So, the strings should end up like:

"20 - 10"             * or better *  "10"
"180 + 50"            * or better *  "230"
"20 + 10 - 180 - 50"  * or better *  "-200"

I think I should use regular expressions for this. Any ideas as to how I can accomplish this?

Edit: What I need is pretty much something that can take [Current.Something]s and replace them with relevant values. I know I can do it with simple string operations but I just wonder if there's a short way to do this.

hattenn
  • 4,371
  • 9
  • 41
  • 80
  • 2
    Perhaps look into using a library like [NCalc](http://ncalc.codeplex.com/) or [FLEE](http://flee.codeplex.com/); they parse expressions and compile them into IL instructions at runtime with full variable resolution. – Chris Sinclair Jan 23 '13 at 15:02
  • possible duplicate of [Is there a string math evaluator in .NET?](http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net) – Bridge Jan 23 '13 at 15:04
  • @ChrisSinclair I only need this for this small job, and the expressions don't get any more complicated than this. Plus, my employers are really not open-minded to using outside libraries unfortunately. – hattenn Jan 23 '13 at 15:07
  • @hattenn That sucks. I'd suggest that you do some analysis then in terms of what _exact_ operators will be used, how lenient syntax would be, and what types are to be used. For example, do you allow decimal numbers in addition to the integers posted? Do you require spaces around your operators or not? Will you be leveraging multiplication/division/exponents/parenthesis, and if so, will you honour BEDMAS/order-of-operations? – Chris Sinclair Jan 23 '13 at 15:12

1 Answers1

1

If you have control of the class that contains the value; you can just add a method called:

int getValue(string fromThis)
{
    switch(fromThis)
    {
    case "age": {....
}

Then you need to run the text through a text parser (you should be able to create onc fairly easily).

Something like:

string[] newStrings = newString.Split(' ');
if (newStrings.Length < 3)
{
    //error
}
else if (newStrings[0][0] != '[')
{
    //error
}
else
{
    int newValue = 0;
                string fieldString = newStrings[0];// Extract just the part you need.... 
                // I would probably do the above in a method 
    int currentValue = getValue(fieldString);
    int changeValue;
    int.TryParse(newStrings[2], out changeValue);

    switch (newStrings[1])
    {
        case "+":
            {
                newValue = currentValue + changeValue;
                break;
            }
        case "-":
            {
                newValue = currentValue - changeValue;
                break;
            }
        default:
            {
                //error
                break;
            }
    }

    //do something with new value
}

There would be more work for determining what to do for connected statements, but the above should get you going in the right direction. There are slightly cleaner ways to do this using reflection but it's harder to maintain.

Anthony Nichols
  • 1,586
  • 2
  • 25
  • 50
  • Something else to think about is where are you getting the data. If you are extracting the string for let's say an XML file there may be an easier way to do it. – Anthony Nichols Jan 24 '13 at 20:12