[Plugin] Text tools: beforeLast() afterLast()

0 favourites
  • 3 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • I'm working on a text utility plugin, and just wanted to get feedback on some function (C2-expression) names I'm planning to use.


    beforeLast( source , slicer )

    afterLast( source , slicer )

    These return the portion of the source string before or after (respectively) the last occurrence of the slicer substring. If slicer is not found, the source is returned unmodified.

    I think it might be a little more niche than what usually shows up in generic text libraries, but I'm not sure, so I just wanted to double check on the forums to see if anyone knew if there was a conventional name for this functionality. Or if anyone has a suggestion for a better name.


    afterLast( "root/trunk/branch.jpg" , "/" )

    // Slices on: root/trunk / branch.jpg

    // Returns: "branch.jpg"

    beforeLast( "branch.jpg" , "." )

    // Slices on: branch . jpg

    // Returns: "branch"

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can do the same with tokenat(src, index, separator) and tokencount(src, separator) :

    afterLast (src,separator) = tokenat (src, tokencount(src,separator)-1,separator)

    beforeLast (src,separator)=tokenat(src,tokencount(src,separator)-2,separator)

  • Thanks for the reply MadSpy, :)

    Yeah, the afterLast() and beforeLast() functions are kind of a shorthand for stuff that can certainly be done with more functions.

    One of my main reasons for incorporating them into my plugin, is to make certain expressions a bit easier to understand at a glance.

    The afterLast() function especially is pretty straightforward to recreate with built-in text expressions.

    I may not have explained the beforeLast() that clearly, but it returns everything before the last slicer string found, rather than just the previous token.

    So I think

    beforeLast( src , separator )

    is not quite the same as

    tokenat( src , tokencount( src , separator ) - 2 , separator )

    for instance, given

    var src = "root/trunk/branch.jpg"

    var separator = "/"

    beforeLast( src , separator ) // returns "root/trunk"

    tokenat( src , tokencount( src , separator ) - 2 , separator ) // returns "trunk"

    This is actually the same issue I ran into when trying to create that functionality with the built-in text expressions, and the reason I thought it might be handy to have a beforeLast() in particular. Otherwise you have to either get the index of the last token, (which can be tricky with "find()" if there's another identical token elsewhere), and crop from the start of the src string to it's index; or you have to recombine all but the last token, reinserting the separators back in-between the remaining tokens. Both of those are doable, but they start getting a bit convoluted looking compared to the beforeLast() expression.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)