How to erase a word at once?

Asked

Viewed 89 times

1

I am using this method to delete the last character typed in a Textview:

texto = txtTexto.getText().toString();
int length = texto.length();  
txtTexto.setText(texto.substring(0, length - 1));

When a word like sine I want to erase her whole.

  • But you want to delete it wherever it appears, or only if it appears at the end of the string?

2 answers

1


The method can be used String#replace() to eliminate her.

But if you do:

texto = txtTexto.getText().toString();
texto.replace("seno", "");

The above code will not work as expected, it will not modify the calling object because strings (or better, variables) in Java are immutable, then what you need to do is assign the result to a new string or for the same variable as:

texto = txtTexto.getText().toString();
texto = texto.replace("seno", "");

Other similar functions that may be useful to you:

  • `String#replaceFirst() occurs in the first and to replace the last occurrence of a word in text.

1

Use the replace:

texto.replace("seno","")

Browser other questions tagged

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