I am making a calculator with a delete button. I want the delete button to delete one element at a time.
Example, when I press DEL btn, the no.3 will be removed from the EditText.
This is my code for the dlt btn
if (txtResult.length() > 0){
txtResult = txtResult.getText().Remove(txtResult.length() - 1);
if (txtResult.equals("")){
txtResult.setText("0");
}
}
}
What is the equivalent of .Remove from c# to android studio java? Thank you.
CodePudding user response:
You can delete the string with substring
.
Try this:
String a = "AAA";
a = a.substring(0, a.length() - 1);
System.out.println(a); // result: AA
- java string document(you can find some information about substring): https://docs.oracle.com/javase/7/docs/api/java/lang/String.html