Dojo in XPages – 2: String Manipulation

In this post, we’ll take a look at dojo string manipulation utilities. The String utilities are included automatically in XPages — you don’t have to do anything extra in order to use them. You just have to refer to them with the proper hierarchy. Since they’re in the string library, the methods will be called in this form: dojo.string.method()

Dojo in XPages Series

Dojo in XPages — All Blog Posts


The trim function trims whitespace before and after a string. It does not trim redundant space within the string.

Code Result
dojo.string.trim("       trim     this   string      ") trim     this   string

This may seem pointless because there’s a string.trim() method in JavaScript, but it’s not supported in IE8 and below, so the Dojo version works around that and give you a safer cross-browser option, which is one of the main points of using a client-side JavaScript library.


The pad function gives you the ability to easily ensure that a string is at least a certain size. The first parameter is the string to pad. The second parameter is the minimum number of characters. The third (optional) parameter is the character to use to pad the string. The default is 0. The fourth (optional) parameter, when set to true, will pad the string on the right; by default it will pad to the left.

Here are some examples:

Code Result
dojo.string.pad("pad 20", 20) 00000000000000pad 20
dojo.string.pad("pad 20", 20, ' ')               pad 20
dojo.string.pad("pad 20", 20, '^', true) pad 20^^^^^^^^^^^^^^
dojo.string.pad("pad 20", 20, 'ABCDEF', true) pad 20ABCDEFABCDEFABCDEF
  • Example 1 uses the defaults of padding to the left and with a 0.
  • Example 2 pads with a blank space.
  • Example 3 pads to the right with a caret (^)
  • Example 4 is interesting. You can define more than one character to pad the string. When you do that, it will use your pattern and apply it as many times as needed in order to reach the minimum length. It will not stop with a partial instance of the pattern, though, so, as you can see in the last example, it will go longer than your minimum if it needs more space to finish the pattern.


The rep function repeats the specified string the specified number of times.

Code Result
dojo.string.rep("RepeatThis", 5) RepeatThisRepeatThisRepeatThisRepeatThisRepeatThis


The substitute function uses a templated pattern to replace values in the string. The first parameter is a string with placeholders that you intend to replace, in this form ${placeholderName}. The second parameter defines the values to replace in each placeholder, based on matching the name.

Code Result
dojo.string.substitute("Hello ${last}, ${first}", {last: "John", first: "Smith"}) Hello ,
var dollar = '$';
dojo.string.substitute("Hello " + dollar + "{last}, " + dollar + "{first}", {last: "John", first: "Smith"}));
Hello John, Smith

The first example shows how it’s supposed to work. However, the server doesn’t like the dollar signs very much, so it removes them, because it looks to replace code in this format in client-side JavaScript with something computed from the server: ${ }.

The second example works around this limitation and lets the function work within the XPages context. (I also tried to escape the dollar sign, but it threw errors and wouldn’t even load the page.)

All in all, these functions are nothing fancy, but, they can be handy and they’ll save you some time if you need these features.

SSJS Caveat

Keep in mind that Dojo is a client-side JavaScript library, so this code will not work in server-side JavaScript. If you try to refer to Dojo in SSJS, you’ll get a ‘dojo’ not found message.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: