Working with String Variables

In this post I take a look at how JSL can be used to perform string manipulation.

Constructing a String Variable

A string variable is created by enclosing the text within double-quotes.

The script editor will automatically colour code string variables as purple (this behaviour can be customised under preferences).

Escape Sequences

What if the text string itself contains the double-quote character? In this instance the quote character needs to be preceded by a special escape notation that indicates that the character is part of the string and not the delimiter of the string:

If a string requires a large number of double-quotes the use of escape sequences becomes tedious and error prone. Fortunately JSL provides an alternative notation that can be used:

Text within the square brackets can be written without the need to use escape sequences, for example:

Concatenating Strings

Often a string variable is constructed by adding multiple strings together. String addition is referred to as concatenation and is performing using the concatenation operator: ||.

Converting to a String

In the above example the string variable str was constructed from a literal quoted string plus a second variable that contained a string. Sometimes the second variable is numeric, in which case it must be explicitly converted to a string. This can be performed using the Char function:

String Substitution

In the above example it was necessary to concatenate three elements in order to construct the string that had the age variable embedded in it.   Another technique is to use string substitution.

We could write the above string with a place-holder for the age:

And now we can substitute the actual value for the placeholder:

Note that the value being substituted needs to be a string, so in this example the Char function is still required to convert age from a number to a string.

Conditional Logic and String Searches

With numeric values it is often the case that we want to perform some form of conditional logic based on their value:

With string values we can also perform conditional logic:

Or equivalently, using the Match function:

String comparisons are case-sensitive.  JMP has functions to transform case: lowercase, uppercase, and titlecase.  So a more robust comparison would be:

String Length

The number of a characters in a text string can be determined using the Length function:


Extracting Content

To locate portions of text within a string, JMP provides a variety of functions, including word, words, left, right, and substr.

strWord = Word( n, string, <delimiter> )

The word function returns the n’th word within a string.  The default delimiter for each word within the string is a space character.  However, the optional delimiter field can be used to identify an alternate character to be used.


lstWords = Words( string, <delimiter> )

Whereas the word function identifies a single specific word within a string, the words function returns all the individual words as items of a list:

If an empty string is specified as the delimiter then each character of the string is treated as a separate word.

Example 1: isolating individual characters

Example 2: counting the number of words

Example 3: determining the file extension of a file

strLeftMostChars = Left( string, n, <filler> )

The left function can be used to extract the n leftmost characters of a string.  An optional filler character can be specified for instances where the string may be less than n characters in length.

Similarly there is a right function to extract the n rightmost characters.

Example : determining whether a file is a JMP table

strSubstring = Substr( string, offset, <count> )

The substr function returns part of the string composed of count characters starting at position offset.


Pattern Matching

Pattern matching in JSL provides an exceptionally powerful and flexible mechanism for searching and manipulating text strings. Central to pattern matching is the creation of variables that contain pattern definitions. These definitions are then processing by pattern matching functions. I will illustrate the principle based on a scenario that I am currently working on.

I have a column formula that contains a model of the form:


Where K1, K2, N1 and C are parameters that are estimated using the nonlinear platform. I want to inspect the formula for the column and retrieve the K2 value, which represents the activation energy for this kinetic equation.

I can retrieve the formula by sending the getFormula message to the column; this is what it looks like:

Notice that there is a pattern to how the parameter values are specified:

K2 = <k2_value>,

I can describe this pattern by defining a pattern matching variable:

This pattern variable says “find K2 followed by an equals sign, then some arbitrary text, then a comma”.  It also stores the arbitrary text in the variable k2_value. The value is arbitrary in that it doesn’t have a known value, but it is the value I am seeking.

Now I can apply the pattern to a string representation to the formula, check that I have a successful match, and if so then I can convert the string value of k2_value to a number:


2 thoughts on “Working with String Variables”

  1. Hey !

    good post on the string operations!

    Question on what i’ve been wondering while: is there a way without looping to perform string operations with lists?

    firstName = {“John”, “Derrick”,”Klaus”};
    lastName = {” Nicklaus”, ” Woods”, ” Cabrera”}

    // And the concatenating these lists with “||”
    firstName || lastName = {“John Nicklaus”, “Derrick Woods”, “Klaus Cabrera”}

    With the current logic, the list is exanded to 6 element list

    Making new column for this in the table is easy to achieve too, but not really proper way..

Leave a Reply

Your email address will not be published. Required fields are marked *