2

I have a TextBox where a user can enter a calculation, such as 100+200 on his keyboard.

How can I break the string into three parts, like:

string mySum = "200+800"; //Just and example of what he may enter into the textbox
int Operator = mySum.IndexOf('+');
string TheOperator = "+";
string part1 = (mySum.Substring(1, Operator - 1));
mySum.Remove(int.Parse(part1), Operator);
string part2 = (mySum);

//Calculate

int Answer = int.Parse(part1) + TheOperator + int.Parse(part2);
Messagebox.Show(Asnwer.toString()); //Message box should display 1000

Firstly I know this is wrong, but I am highly unsure how to do this. Ive looked everywhere but I can't find anything related directly to this

Aidan
  • 23
  • 5

5 Answers5

2

simple as this , make use of Split function

split by char

string[] words = s.Split('+', StringSplitOptions.RemoveEmptyEntries);

Split by char array , when there is more than one char

char[] delimiters = new char[] { '+', '*' };
    string[] parts = value.Split(delimiters,
                     StringSplitOptions.RemoveEmptyEntries);

you can also make use of regular expression like this

string value = "cat\r\ndog\r\nanimal\r\nperson";
    // Split the string on line breaks.
    // ... The return value from Split is a string array 
   string[] lines = Regex.Split(value, "\r\n");
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
2

You should use string.Split as follows:

// ...

var parts = mySum.Split('+');
var part1 = parts[0]; // 200
var operatorString = parts[1]; // +
var part2 = parts[2]; // 800

// ...
Ian CT
  • 1,301
  • 2
  • 12
  • 22
  • This will actually produce the following - part1 = "2", operatorString = "0" part2 = "0". I can see you probably meant to use parts[0], parts[1] and parts[2] instead, but the split character is also not included in the output, so parts would only have 2 elements, not 3. – Paul McLean Mar 03 '15 at 06:49
  • @Aidan - i think i am fastest as per SO timing ...any ways....at lest you accepted answer....:) – Pranay Rana Mar 03 '15 at 11:40
  • @PranayRana Haha, I'm sorry. Im still very new with C# syntax, Ian Chu's answer looks the most simple and did what I wanted it to do. Although thanks for answering :) edit: If I had enough reputation, I would have upvoted your answer as well <3 – Aidan Mar 03 '15 at 11:54
  • Fastest answer doesn't always mean the best answer :) – Ian CT Mar 03 '15 at 13:49
1

If you just want to do some simple calculation, try this

From Evaluating string "3*(4+2)" yield int 18:

using System.Data;

DataTable dt = new DataTable();
var v = dt.Compute("200+800","");
Community
  • 1
  • 1
User2012384
  • 4,769
  • 16
  • 70
  • 106
1

split would work here.

using System;

public class Program
{
    public static void Main()
    {
        string mySum = "200+800";
        int totalSum = 0;
        foreach(var op in mySum.Split('+'))
        {
            totalSum += Convert.ToInt16(op);
        }
        Console.WriteLine(totalSum);
    }
}

here is fiddle.

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
1

If you want only calculate you can do like this:

 var result = new System.Data.DataTable().Compute("200+800", null);
 Console.WriteLine(result);

Hope helps

Jamaxack
  • 2,400
  • 2
  • 24
  • 42