Using the split method in triggers

Here’s a short tutorial on the split() string method. It’s so useful it warrants its own post. Let’s say we’ve got a URI, like this:

http://matt_custom.com:8080/foo/bar/baz.jsp

Now, let’s imagine that you want some portion of it, like the ‘bar’ portion of the uri. Here’s one way to do it - IMO the easiest way. Here’s an example from a javascript console:

js> var uri = 'http://matt_custom.com:8080/foo/bar/baz.jsp'
js> uri.split('/')[4]
bar // This is the output, and is exactly what we want...

What just happened? Let’s have a look at this in a bit more detail. The split() call splits up a string against a specific sub-portion of that string and returns an array.

Now, recall that an array is just a list. Each entry has an ‘index’, or position in that list. The index count starts at zero:

var myarray = [0, 1, 2, 3] // An array of 4 elements, the numbers zero - three. 

So at the first index - starting at zero, we get the number 0. At index one, we get the second index, the number 1, and so on…

Here’s a more practical and simple example, where we’ll split up an FQDN on the ‘dot’:

js> var fqdn = 'zero.one.two.three.com';
js>     var return_array = fqdn.split('.') // Here, we'll split on the dot.
js> return_array
zero,one,two,three,com // Now, we've got an array of five individual strings. 

So here again, we’ve got our array. Let’s now print out just the first item.

js> return_array[0] // this will return the first element of the array, at index 0.
js> zero // the output.

So now, going back to our original example of that nasty URI, it doesn’t look so difficult now:

js> var return_array = uri.split('/');
js> return_array
http:,,matt_custom.com:8080,one,two,three.jsp
 0   1        2              3   4    5

I’ve numbered the indexes (or tried to) below each element. Note that element 1 is an empty placeholder, because there’s nothing between the ‘//’ slashes in ‘http://’.

Let’s now split it on the “:” - what do you think the array will look like?

js> uri.split(':')
http,//matt_custom.com,8080/foo/bar/baz.jsp 
//it's three elements now, the 'http' portion, the port, and then everything else.

It turns out that this is one of the more handy methods at your disposal in trigger world, so be sure and take advantage of it!

As a parting note, remember this: anything in a trigger that returns a string will have a split() method. For example:

HTTP.uri.split('/') 
HTTP.host.split('.')
...etc.

Hopefully you found this useful please post a comment if you have questions.

–Matt

I get an error when trying to use split.
{code}

            payload = HTTP.payload;              
            temp = payload.split("DeveloperKey>");
            if (temp[1]){
                devkey = temp[1].split("<");
                devkey = devkey[0];
                temp = null;
                Flow.store.DeveloperKey = devkey;
            }

I get ‘Uncaught TypeError: payload.split is not a function’

HTTP.payload is not a String; it’s a Buffer. See the Trigger API guide.