• Login

dojo.trim

Status: Final
Version: 1.4
Available: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.

Basic Usage

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

Examples:

1
2
3
4
5
6
7
8
9
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");

You can see the result here:

Examples of dojo.trim().

<script>
  function show(str){
    return "|" + dojo.trim(str) + "|";
  }
  dojo.addOnLoad(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");
  });
</script>

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>

dojo/trim (last edited 2009-11-03 07:50:33 by eugene)