I have a string input like:
1 3 4 1 2
I want to Sum
the number into integer. I tried the following code:
using System;
public class Program
{
public static void Main()
{
string input2 = "1 3 4 1 1";
string value2 = input2.Replace(" ", "+");
int val = int.Parse(value2);
Console.WriteLine(val);
}
}
But it is not correct. Does anyone have an idea for this? Thank you.
You can Split input2
with a space character into an array
then use system.linq.enumerable.sum()
string input2 = "1 3 4 1 1";
var result= input2.Split(' ').Sum(x=> int.Parse(x));