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  ");
});

codePointAt()

Return the UTF-16 code point value for the character at the specified index in a string. Returns undefined if position is out of bounds. Throws a TypeError if string is null or undefined.

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

Error in the documentation? Can’t find what you are looking for? Let us know!