Home  >  Article  >  Web Front-end  >  Does javascript have a trim method?

Does javascript have a trim method?

青灯夜游
青灯夜游Original
2022-04-13 15:45:322130browse

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()

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();

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!

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
Previous article:How to clear timer in es6Next article:How to clear timer in es6