Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last line from string

How do I remove the last line "\n" from a string, if I dont know how big the string will be?

var tempHTML = content.document.body.innerHTML;
var HTMLWithoutLastLine = RemoveLastLine(tempHTML);

function RemoveLastLine(tempHTML)
{
   // code
} 
like image 277
user1534664 Avatar asked Nov 30 '12 14:11

user1534664


People also ask

How do I remove the last 3 characters from a string?

slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original string.


1 Answers

Try:

if(x.lastIndexOf("\n")>0) {
    return x.substring(0, x.lastIndexOf("\n"));
} else {
    return x;
}
like image 199
rekire Avatar answered Oct 23 '22 18:10

rekire