-1

How i can evaluate this string with this delimiter #c"stringtosum"#g

code1,code2,#c55+35+97#g,coden,code3,code4,#c44+25+07#gcoden

55+35+97=187

44+25+07=76

and output this

code1,code2,#c187#g,coden,code3,code4,#c76#gcoden

i am using this two string ,#c55+35+97#g ,#c44+25+07#g ,, im tring to evaluate (sum) this two

55+35+97=187

44+25+07=76

and output this

code1,code2,#c187#g,coden,code3,code4,#c76#gcoden

i have multiple string to sum

Cœur
  • 37,241
  • 25
  • 195
  • 267
jhonny625
  • 266
  • 2
  • 14

4 Answers4

1

First you can use a regular expression like #c([^#]*)# to find the matches. Then you just need to take the string in the grouping, split on the plus sign, parse the values to integers, and do the sum, like this.

string input = "code1,code2,#c55+35+97#g,coden,code3,code4,#c44+25+07#gcoden";

string output = Regex.Replace(
    input, 
    "#c([^#]*)#", 
    m => "#c" + m.Groups[1].Value.Split('+').Sum(int.Parse) + "#");

Console.WriteLine(output);

Outputs

code1,code2,#c187#g,coden,code3,code4,#c76#gcoden

juharr
  • 31,741
  • 4
  • 58
  • 93
1

please help me

well...since you asked nicely.

 var str = "code1,code2,#c55+35+97#g,coden,code3,code4,#c44+25+07#g,coden";
        var parts = str.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);

        var builder = new StringBuilder();

        for (var i = 0; i < parts.Length; i++)
        {
            var temp = parts[i];

            if (temp.StartsWith("#c") && temp.EndsWith("#g"))
            {
                var nums = temp.Substring(2, temp.Length - 4).Split(new[] { "+" }, StringSplitOptions.RemoveEmptyEntries);
                temp = $"#c{nums.Sum(int.Parse)}#g";
            }
            builder.Append(temp);
            if (i != parts.Length-1)
                builder.Append(",");
        }

        var output = builder.ToString();
Mike_G
  • 16,237
  • 14
  • 70
  • 101
  • 1
    I would go with @juharr answer. It is cleaner, and I think i altered your initial string at the end there. Wasn't sure if it was a typo – Mike_G Oct 19 '16 at 18:30
1

You can use Regular Expressions to identify the sections of the string that you would like to parse and evaluate and then you can bring in the Jace.NET NuGet package to evaluate the calculations. This method would work for all types of arithmetic operations instead of only summations:

var inputString = "code1,code2,#c55+35+97#g,coden,code3,code4,#c44+25+07#gcoden";
var calcEngine = new CalculationEngine();       // add "using Jace;" at the top of your code file to bring in the CalculationEngine
var outputString = Regex.Replace(inputString, "#c([^#]*)#g", m => "#c" + calcEngine.Calculate(m.Groups[1].Value).ToString() + "#g");
sam2929
  • 479
  • 7
  • 14
-1

I would suggest looking into regular expressions (regex). Regex allows you to parse through a string and pick out certain parts. From there, you can add them and then substitute them back into your string.

As an example, "\d*+\d*" will allow you to select the two substrings "55+35+97" and "44+25+07"

danielku97
  • 60
  • 8