Home > Web Front-end > JS Tutorial > How to remove spaces before and after a string in jquery

How to remove spaces before and after a string in jquery

WBOY
Release: 2021-11-23 16:40:02
Original
4472 people have browsed it

In jquery, you can use the "$.trim()" function to remove spaces before and after a string. This function will remove all whitespace characters (newlines, spaces, and tabs) at the beginning and end of the string. symbol), the syntax is "$.trim(string variable)".

How to remove spaces before and after a string in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

How to remove spaces before and after a string in jquery

In jquery, you can use the $.trim() function to remove whitespace characters at both ends of a string. .

$.trim() function removes all newline characters, spaces (including consecutive spaces) and tab characters at the beginning and end of the string. If these whitespace characters are in the middle of the string, they are retained and not removed.

The syntax is:

$.trim( str )
Copy after login

The example is as follows:

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
     
<pre id="original">
Copy after login
Copy after login
<script> $(function () { var str = " 前面有空格 后面有空格 "; $( "#original" ).html( "Original String: &#39;" + str + "&#39;" ); $( "#trimmed" ).html( "$.trim()&#39;ed: &#39;" + $.trim(str) + "&#39;" ); }) </script>

Output result:

How to remove spaces before and after a string in jquery

Related video tutorials Recommended: jQuery video tutorial

The above is the detailed content of How to remove spaces before and after a string in jquery. 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