Home >Web Front-end >JS Tutorial >How to use trim method in javascript

How to use trim method in javascript

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-16 10:48:0211702browse

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.

How to use trim method in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn