1

I want to simply remove the last character of a string that is of an undefined length. This is what I have:

var str = txtBx1.Text;
txtBx2.Text = (str.TrimEnd(str[str.Length - 1]));

It works great, when the last two characters are unique. However, when the last two or more characters are the same, all of the repeating characters are removed.

If txtBx1.Text = '123456789' then txtBx2.Text will be '12345678'

If txtBx1.Text = '199999999' then txtBx2.Text will be '1'; it needs to be '19999999'

How can I simply remove the last character of a string that is of an undefined length?

User 12345678
  • 7,714
  • 2
  • 28
  • 46

2 Answers2

2

Use Substring method

txtBx2.Text = str.Substring(0, str.Length - 1);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

Use substring:

str.Substring(0, str.Length - 1);
brz
  • 5,926
  • 1
  • 18
  • 18