Just a quick post (yes, I know I still owe you a PART 2 for the accumulating data post) ..
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!)
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.
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?)
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.
findOneOf('this is a test', ' ', -1) // returns 10; the position of the last space
Keep on Qlikin’
2 thoughts on “Did you get the memo? … finding the subField of a string”
I like that one 🙂
It even works with -2 as second last and so on
Wow – that’s a great tip Tamara!
Comments are closed.