I have a string like str = " 4+ 6 * 30";
. I have to perform an arithmetic operation on this using C#; My solution to this problem is as follows.
string temp = " 4 + 6 * 5";
int firstNaum = 0;
int secondNum = 0;
int ThirdNum = 0;
int finalResults = 0;
//Spliting strings
string[] withoutOperator = temp.Split('\t',' ','*' , '+');
//Iterating strings
int counter = 0;
foreach (var res in withoutOperator)
{
if (!string.IsNullOrEmpty(res) && counter ==1)
{
firstNaum = Convert.ToInt32(res);
}
if (!string.IsNullOrEmpty(res) && counter== 4)
{
secondNum = Convert.ToInt32(res);
}
if (!string.IsNullOrEmpty(res) && counter == 7)
{
ThirdNum = Convert.ToInt32(res);
}
counter += 1;
}
finalResults = firstNaum + secondNum * ThirdNum;