How to remove everything after a specific character

I have a line which is below:

Documents for 047-428583 > FOLDER A > FOLDER D

      

I want to delete > FOLDER D

by performing a specific operation.

I tried using the substring below but it removes everything after >

 var data = $("#extend").text();
 $("#extend").text(data.substring(0, data.indexOf('>')));

      

I went through this one , but in my case, I have multiple identical symbols so I cannot use that. I think!

+3


source to share


3 answers


Use lastIndexOf instead:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf



$("#extend").text(data.substring(0, data.lastIndexOf('>') - 1));

      

+3


source


You can split the value text()

by character >

and then remove the last element in the resulting array:

$("#extend").text(function(i, val) {
    var arr = val.split('>');
    arr.pop();
    return arr.join(' > ');
});

      



Sample script

+2


source


You can split and then join again:

 var data = $("#extend").text().split('>').pop();
 data.join(" > ");

      

EDIT

As the comment says, the pop () method returns the final string and this code is incorrect. Correct answer: @Rory McCrossan writes: fooobar.com/questions/2248259 / ...

0


source







All Articles