dojo/string

dojo/string is a module that provides some simple string manipulation utilities.

Usage

require(["dojo/string"], function(string){
  var a = string.pad("pad me", 10);
  var b = string.rep("dup", 10);
  var c = string.substitute("${replace} - ${me}", { replace: "foo", me: "bar" });
  var d = string.trim("  trim me  ");
});

pad()

Pad a string to guarantee that it is at least size length by filling with the character ch at either the start or end of the string. Pads at the start, by default. The signature of the method is:

NameTypeDescription
textString

the string to pad

sizeInteger

length to provide padding

chString

character to pad, defaults to '0'

endBoolean

adds padding at the end if true, otherwise pads at start

rep()

Repeats a string a certain number of times. The signature of the method is:

NameTypeDescription
strString

the string to replicate

numInteger

number of times to replicate the string

substitute()

substitute() is a workhorse and the basis for Dijit's templating. It performs parameterized substitution in the form of ${name} with a variety of advanced options. An object is provided as the hashtable to lookup when doing these substitutions. The expression in the curly braces may be a simple property, like name or a dotted expression like data.employee.name. The expression may be further qualified by a colon and the name of a format function, to run the output each lookup through a property, such as mylib.formatName. A this reference may be provided for the format function, otherwise it will be scoped to the global namespace. Lastly, an optional transform function can be run on all properties just prior to substitution, such as one to escape HTML entities.

The signature of the method is:

NameTypeDescription
templateString

a string with expressions in the form ${key} to be replaced or ${key:format} which specifies a format function. keys are case-sensitive.

mapObject|Array

hash to search for substitutions

transformFunction

a function to process all parameters before substitution takes place, e.g. mylib.encodeXML

thisObjectObject

where to look for optional format function; default to the global namespace

trim()

trim() trims whitespace off both ends of a string.

This will default to the ES5 String.prototpye.trim if available, otherwise it will utilise a more performant, but not very compact version of the trim(), which is different than the trim() which is included in .

The signature of the method is:

NameTypeDescription
strString

String to be trimmed

Examples

An example of pad().

require(["dojo/string", "dojo/dom", "dojo/domReady!"],
function(string, dom){
  dom.byId("output").innerHTML = string.pad(dom.byId("input").innerHTML, 6);
});
<div id="input">123</div>
<div id="output"></div>

An example of rep().

require(["dojo/string", "dojo/dom", "dojo/domReady!"],
function(string, dom){
  dom.byId("output").innerHTML = string.rep("Pete and Repeat went out in a boat, Pete fell in. ", 5);
});
<div id="output"></div>

An example of substitute().

require(["dojo/string", "dojo/dom", "dojo/domReady!"],
function(string, dom){
  dom.byId("output").innerHTML = string.substitute(dom.byId("input").innerHTML, { replace: "foo", me: "bar" });
});
<div id="input">${replace} has the hots for ${me}</div>
<div id="output"></div>

An example of trim().

require(["dojo/string", "dojo/dom", "dojo/domReady!"],
function(string, dom){
  dom.byId("output").innerHTML = string.trim(dom.byId("input").innerHTML);
});
<pre id="input">   I got space!   </pre><br /><br />
<pre id="output"></pre>
pre { border: 2px solid black; display: inline; padding: 3px; }

See Also