CallFire Solutions

Article 1: JavaScripting and Manipulating Strings

Follow

Many times, the IVR will require us to get a string and either break it into usable subparts or, sometimes, insert other characters, including spaces, into the string.

The tools we will use for this are three Java functions, '.split()', '.join()', and '.substring()'.

.Split()

'.split()' breaks apart the string at the spot described in the argument. For example, '.split("")' says to break apart the string between every element; '.split("|")' says to break it apart every time it reaches the "pipe" character ( | ); '.split(",")' says to break it apart every time it reaches a comma.

.Join()

'.join()' puts the string back together with whatever character is indicated by the argument inserted between the previously split elements. For example, '.join("|")' puts the string back together with the "pipe" as a delimiter;

'.join(" ")' puts the string back with a space between the elements. This is used to "explode" a number for correct text-to-speech playback (see below).

.Substring()

'.substring()' grabs a particular part of the string. The argument should say which place in the string to start with, and how many places to grab. Keep in mind that when counting places in strings or arrays, you always start with '0' (zero). So, '.substring(0,3)' would begin with the first element of a string, and grab the first three elements. An example would be the area code from a 10-digit phone number. See below for the real-world examples.

The last piece of coding associated with this is the array place reference. You can grab a particular part of a string array by referencing its place in the array in brackets following the split argument. For example, '.split(",")[1]' says to split the array at the comma delimiter and take the second item in the array (since the count starts with "0", "1" is the second place).

Here are some real-world examples

To "explode" a number for correct TTS playback, we would need to split each element (number) in the string and insert a space between them. Assuming our "number" is the variable 'acctNum', our tag would look like this:

<setvar name="setvar_explNum" varname="explAcctNum">${acctNum}.split("").join(" ");</setvar>

(The semicolon ';' is used to end a Java command.)

Suppose we sent an account ID using a get tag, and got back a variable ('myResult') that contained three elements — the user name, account balance, and due date — separated by a 'pipe' ( | ) delimiter. We could take the results of the Get Tag ('myResult') and split it into the three elements at the pipe delimiter, and place each element into its own variable:

1.<setvar name="setvar_user" varname="userName">’${myResult}’.split("|")[0];</setvar>
2. <setvar name="setvar_balance" varname="acctBal">’${myResult}’.split("|")[1];</setvar>
3. <setvar name="setvar_dueDate" varname="dueDate">’${myResult}’.split("|")[2];</setvar>

We can also use javascript to grab the area code from the caller ID. It is unpredictable whether the caller ID contains a “+” sign, the numeral “1,” or even both of those, so we’ll first need to get the 10-digit phone number. Since that will be the 10 digits on the right end of the string, we can use the “.substr” to get the last 10 digits, as follows:

<setvar name="setvar_callerID" varname="callerid">’${call.callerid}’.substr(-10);</setvar>

The variable we just created, “callerid,” now contains just the 10-digit phone number. We can get the first three digits – the area code – as follows:  

 

 

Have more questions? Submit a request

Comments