Home > Web Front-end > JS Tutorial > body text

How to use trim method in javascript

醉折花枝作酒筹
Release: 2023-01-06 11:17:34
Original
11668 people have browsed it

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()
Copy after login

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
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template