Home > Web Front-end > Front-end Q&A > Does javascript have a trim method?

Does javascript have a trim method?

青灯夜游
Release: 2022-04-13 16:41:47
Original
2142 people have browsed it

There is trim method in javascript. trim() is a string processing method built into JavaScript, which is used to remove whitespace characters at the beginning and end of a string. The syntax is "string.trim()"; the whitespace characters that the trim method can delete include spaces and tabs. character, line feed character, carriage return character, etc.

Does javascript have a trim method?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There is a trim method in javascript.

The trim() method is used to remove leading and trailing whitespace characters from a string. The syntax is as follows:

string.trim()
Copy after login

trim() method can remove the following whitespace characters:

  • " ": ordinary space characters;

  • "\t": tab character;

  • "\n": line feed character;

  • "\r": carriage return character;

  • "\0": null byte character;

  • "\x0B": vertical tab character.

The trim() method does not change the original string.

trim() method is not applicable to null, undefined, Number types.

JavaScript version: ECMAScript 5

Return value: Returns removing leading and trailing spaces String

Example:

function f() {
  var str1 = ` 123
  `;
  console.log(str1);
  console.log(str1.trim());
  var str2 = `\n23\n4 `;
  console.log(str2);
  console.log(str2.trim());
}
f();
Copy after login

Does javascript have a trim method?

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of Does javascript have a trim method?. 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