dojo.trim

since:V1.2
Author:Eugene Lazutkin

This function implements a frequently required functionality: it removes white-spaces from both ends of a string. This functionality is part of ECMAScript 5 standard and implemented by some browsers. In this case dojo.trim delegates to the native implementation. More information can be found here: String.trim() at MDC.

Dojo’s implementation was informed by Steven Levithan’s blog post. We chose to implement the compact yet performant version. If your application requires even more speed, check out dojo.string.trim, which implements the fastest version.

Usage

dojo.trim accepts the only argument: a string to be trimmed.

[ Dojo 1.7+ (AMD) ]

require(["dojo/_base/lang"], function(lang){
  function show(str){
    return "|" + lang.trim(str) + "|";
  }

  var output1 = show("   one");
  var output2 = show("two ");
  var output3 = show("   three ");
  var output4 = show("\tfour\r\n");
  var output5 = show("\f\n\r\t\vF I V E\f\n\r\t\v");
});

[ Dojo < 1.7 ]

function show(str){
  return "|" + dojo.trim(str) + "|";
}

var output1 = show("   one");
var output2 = show("two ");
var output3 = show("   three ");
var output4 = show("\tfour\r\n");
var output5 = show("\f\n\r\t\vF I V E\f\n\r\t\v");

Examples

Examples of dojo.trim().

function show(str){
  return "|" + dojo.trim(str) + "|";
}
dojo.ready(function(){
  dojo.byId("output1").innerHTML = show("   one");
  dojo.byId("output2").innerHTML = show("two ");
  dojo.byId("output3").innerHTML = show("   three ");
  dojo.byId("output4").innerHTML = show("\tfour\r\n");
  dojo.byId("output5").innerHTML = show("\f\n\r\t\vF I V E\f\n\r\t\v");
});

Minimalistic HTML for our example.

<p id="output1"></p>
<p id="output2"></p>
<p id="output3"></p>
<p id="output4"></p>
<p id="output5"></p>

Table of Contents

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