Documentation
dojo/string¶
Contents
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:
Name | Type | Description |
---|---|---|
text | String | the string to pad |
size | Integer | length to provide padding |
ch | String | character to pad, defaults to '0' |
end | Boolean | 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:
Name | Type | Description |
---|---|---|
str | String | the string to replicate |
num | Integer | 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:
Name | Type | Description |
---|---|---|
template | String | a string with expressions in the form |
map | Object|Array | hash to search for substitutions |
transform | Function | a function to process all parameters before substitution takes place, e.g. mylib.encodeXML |
thisObject | Object | 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 dojo/_base/lang.
The signature of the method is:
Name | Type | Description |
---|---|---|
str | String | 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¶
- dojo/_base/lang::trim() - Base Dojo trim()