-3

I've literally just started looking at c# like an hour ago and i thought i would try and make a calculator i have one problem and this is the only problem.

I have a way of getting the value of the first value being entered but have no other way to figure out the second value without it showing the entire string being entered.

private void buttonEQ_Click(object sender, EventArgs e)
{

   string valueTWO;
   valueTWO = textBox1.Text;
   Console.WriteLine(valueTWO);

}

So whatever the person enters it will just log it out say 100-50 for example how do i programmatically tell the program to 'get everything after the -' if possible which im sure it is.

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
JkenGJ
  • 35
  • 6
  • 3
    I would recommend you go through a C# tutorial, otherwise you will have a ton of questions like these and these types of questions are not good questions for StackOverflow. For your specific question, google 'C# substring'. – user469104 May 02 '16 at 16:15
  • i'll have a look at it, i just didn't know what it was called. Thanks – JkenGJ May 02 '16 at 16:16
  • Or Google for regular expressions. Do you know other languages? How would you parse the inserted expression? If you don't know what to do, you have a different problem than how to do it. – Alberto Chiesa May 02 '16 at 16:16
  • Ye i do know how to. I know abit in javascript thats where i picked up all the basics – JkenGJ May 02 '16 at 16:18
  • The String method Split or the Regex method Split is probably what you want. If you are familiar with python Split is similar to split in python. https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx – hft May 02 '16 at 16:22
  • Note that question as asked has more or less nothing to do with actual stated goal (calculator) which is covered in many questions like http://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18 or intro chapters to expression parsing (if one wants to do it by hand). – Alexei Levenkov May 02 '16 at 16:33

3 Answers3

0

Try this

 private void buttonEQ_Click(object sender, EventArgs e)
    {

        string valueTWO;
        valueTWO = textBox1.Text;
        string[] s = valueTWO.Split('-');
        // value before "-"
        Console.WriteLine(s[0]);
        // value after "-"
        Console.WriteLine(s[1]);

    }
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
0
string n = s.Split('-').FirstOrDefault();
suulisin
  • 1,414
  • 1
  • 10
  • 17
0

What you are looking for is the split-Method:

string[] textBoxSplitted = textBox1.Text.Split('-');

string valueTwo = textBoxSplitted[1]; //Second result (the one after the dash)

Be aware of that this may fail if the user did not follow the format 'XXX-YYY', so you may check if textBoxSplitted.Length == 2.

Marco de Abreu
  • 663
  • 5
  • 17