How to remove the value of the last Textbox position with Substring?

Asked

Viewed 1,331 times

2

How to remove the value of the last position of the TextBox with the Substring?

Suppose the TextBox receive a value with this mascara = 0,00%, however, I just want to save the numbers and not the character percent, how do I?

  • Is a textbox or a maskedTextbox?

2 answers

3


Assuming really only the last %, that nothing has been typed wrong, and usually something can be, a lot can go wrong if it’s not as expected, and validating everything takes work, then this would be it:

texto = texto.TrimEnd('%');

If you want the last one, it doesn’t matter what:

texto = texto.Substring(0, texto.Length - 1);

I put in the Github for future reference.

  • Thank you very much Maniero, solved.

2

You can also use the Replace to remove any sign or character from your string.

Say:

String texto = "0,00%";

// Retirando o sinal de porcentagem
texto = texto.Replace("%","");
Resultado: 0,00

// Retirando a virgula
texto = texto.Replace(",","");
Resultado; 000%

//retirando a virgula e o sinal de porcentagem
texto = texto.Replace("%","").Replace(",","");
Resultado: 000

The Replace exchange what is between the first two quotation marks with what is between the last two quotation marks = . Replace("first","last")

You can use the Replace several times chained in the same string.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.