In JavaScript, the trim method is used as "String object or string.trim()". The trim method removes leading spaces, trailing spaces and line terminators from the string. This method does not modify Original string.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Remove leading spaces, trailing spaces and line terminators from a string.
Syntax
stringObj.trim()
Parameters
stringObj
Required. String object or string. The trim method does not modify the string.
Return Value
The original string with leading spaces, trailing spaces, and line terminators removed.
Remarks
The characters removed include spaces, tabs, form feeds, carriage returns and line feeds. For a complete list of whitespace and line terminators, see Special Characters (JavaScript).
For an example showing how to implement your own trimming methods, see Prototypes and Prototypal Inheritance.
The following example demonstrates the use of trim method.
var message = " abc def \r\n "; document.write("[" + message.trim() + "]"); document.write("<br/>"); document.write("length: " + message.trim().length); // Output:// [abc def]// length: 7
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to use trim method in javascript. For more information, please follow other related articles on the PHP Chinese website!