简体   繁体   中英

How do i target a certain part of a string C#

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.

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]);

    }
string n = s.Split('-').FirstOrDefault();

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM