Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the last character of a textbox text?

Tags:

c#

asp.net

I am a beginner in C#. I am making a web calculator like Microsoft Desktop calculator with the help of asp.net. But I'm stuck at one place. My code for Plus, minus, multiply or div is like:

protected void btnPlus_Click(object sender, EventArgs e)
{
    if (txtBox1.Text.EndsWith("+"))
    {
        txtBox1.Text = txtBox1.Text;
    }
    else
    {
        txtBox1.Text = txtBox1.Text + "+";
        ViewState["Operation"] = "+";
    }
}

But I want to check this condition for all operations like minus, multiply and divide. I don't want Plus, Minus, Multiply or Div signs appear in the textbox.

like image 220
Rakshanda Khan Avatar asked Jan 19 '16 09:01

Rakshanda Khan


People also ask

How do I get the last character of a string in C#?

You can also get the last character by using LINQ, with myString. Last() , although this is likely slower than the other answers, and it gives you a char , not a string . Save this answer.

How do you remove the last 3 characters of a string in Java?

Using String.substring() method The recommended approach is to use the substring() method provided by the String class. The idea is to take a substring from the beginning of the string and consider all the characters in it except the last n characters.

How do I remove the last character of a string?

The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.


2 Answers

You can store all your operators in a string constant and check if the last character is contained in that string:

private const string OPERATORS = "+-/*";
protected void btnPlus_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txtBox1.Text) || // check if string is empty
        OPERATORS.Contains(txtBox1.Text.Last())) // or if last character is a operator
    {
        txtBox1.Text = txtBox1.Text;
    }
    else
    {
        txtBox1.Text = txtBox1.Text + "+";
        ViewState["Operation"] = "+";
    }
}
like image 57
René Vogt Avatar answered Sep 24 '22 09:09

René Vogt


You can do something like the following:

  1. Extract last character
  2. Based on the character assign operator to the view state
  3. If it is any operator then remove them from the textbox
  4. Finally do the operation

     if (txtBox1.Text != "")
            {
                char last_char = txtBox1.Text[txtBox1.Text.Length - 1];
                switch (last_char)
                {
                    case '+':
                        ViewState["Operation"] = "+";
                        txtBox1.Text.Remove(txtBox1.Text.Length - 1);
                        break;
                    case '-':
                        ViewState["Operation"] = "-";
                        txtBox1.Text.Remove(txtBox1.Text.Length - 1);
                        break;
                    // do the same for all operators
                    default:
    
                        break;
                }
            }
    
like image 20
sujith karivelil Avatar answered Sep 21 '22 09:09

sujith karivelil