String.lastIndexOf()

Posted on
  • Hey, lastIndexOf seems to be used now and then by some scripts, but is not supported yet. Will it be?
    -Stev

  • I doubt it will be, and you can define your own function or just use:

    var str = 'Hello planet earth, you are a great planet.';
    var search = 'planet';
    
    var lastIndex = (str.split('').reverse().join('').indexOf(search.split('').reverse().join('')) - str.length + search.length) * (-1)
    

    Please test this, I haven't :P

  • Just added - it'll be in version 1.58 :)

  • Appreciated :)

  • @randunel: Hey, thanks. Since one can access Strings on a character level, one can basically implement anything. But given that this is a somehow common function, other people might also miss it. So, might help to have it directly included in the distribution.

    Funnily enough I came across it in a function that actually did not need it :)
    -Stev

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

String.lastIndexOf()

Posted by Avatar for Stev @Stev

Actions