How to remove the last new line (\n)

Viewed 9731

I have the code below. the if condition is true and it goes into the line inside the IF but when I inspect answerText later, it still has the new line at the end

if(answerText.endsWith("\n")){
     answerText = answerText.substring(0, answerText.length()-2);           
}
2 Answers
org.apache.commons.lang3.StringUtils.stripEnd( "text\n\n\n" , "\n" );

Removes a substring only if it is at the end of a source string, otherwise returns the source string

Related