I suggest overwriting the TextView
class with your own implementation:
public class CustomTextView extends TextView{
// Overwrite any mandatory constructors and methods and just call super
public void removeLastLine(){
if(getText() == null) return;
String currentValue = getText().toString();
String newValue = currentValue.substring(0, currentValue.lastIndexOf("\n"));
setText(newValue);
}
}
Now you could use something along the lines of:
CustomTextView textView = ...
textView.removeLastLine();
Alternatively, since you seem to be looking for a one-liner without creating a String temp
for some reason, you could do this:
textView.setText(textView.getText().toString().replaceFirst("(.*)\n[^\n]+$", "$1"));
Regex explanation:
(.*) # One or more character (as capture group 1)
\n # a new-line
[^\n] # followed by one or more non new-lines
$ # at the end of the String
$1 # Replace it with the capture group 1 substring
# (so the last new-line, and everything after it are removed)
Try it online.