I have a checkbox named Chocolate in my app. When it is checked it prints "Chocolate added!\n". But when i uncheck it after checked, and replace it with "" (no string) then it gets replaced on a new line. And the no. of lines keep on increasing on each check/uncheck. So my question is How to replace the whole string with a backspace? Such that next item should be printed instead of Chocolate on the same line.
public void onCheck(View checkView) {
String checkMessage = "";
boolean checked = ((CheckBox) checkView).isChecked();
checkMessage += "\n";
switch (checkView.getId()) {
case R.id.chocolate:
if (checked) {
checkMessage += "Chocolate added.\n";
}
else {
checkMessage = checkMessage.replaceAll("Chocolate added.\n", "");
}
break;
}
checkMessage += "\n";
...
Your problem here is the lines that have: checkMessage += "\n"; This seems from your code, to add a newline to the string checkMessage, regardless of whether the checkbox is checked. Since you seem to have two of them, the number of lines will increase by 2 on every check/uncheck. So unless there is a use for them, deleting them will stop the lines from increasing.
Alternatively, to answer your question directly, the java escape sequence for a backspace is \b, however I do not know if adding that will solve your problem.