Did you get the memo? … finding the subField of a string

February 25th, 2016 by admin Leave a reply »

lumbergh

Just a quick post (yes, I know I still owe you a PART 2 for the accumulating data post) ..

Situation

I am dealing with a string which has an arbitrary number of words in it. Each word is separated by a space. I want to get the last word. (ahem!)

Solution

I was not aware of this, nor do I see it in the documentation, but by feeding -1 as the index to subField(text, delimiter, index) we get the last subField.

Example:

subField('this is a test', ' ', -1)    // returns 'test'

subField('this is yet another in a big test', ' ', -1)  // also returns 'test'

 

Using this in a production code I would probably play it safe by wrapping a trim() function around my text string in case there are trailing spaces:

subField(trim([Some Field]), ' ', -1)

It’s quite possible everyone already knows about this and I didn’t get “the memo” … but just in case you find it useful – have fun!

(uhhh… and I’ll go ahead and make sure you get another copy of that memo, umm-kayyyy?)

 

ADDENDUM

Just an update — not surprisingly, this technique also works with findOneOf(..). That is, if you supply -1 as the index to findOneOf(text, delimiter, index) it will return the position of the last instance of the delimiter.

Example:

findOneOf('this is a test', ' ', -1)     // returns 10; the position of the last space
 
 
Keep on Qlikin’
- Bill
 
 

Advertisement

2 comments

  1. Tamara says:

    I like that one :)
    It even works with -2 as second last and so on

Leave a Reply