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
WBOYOriginal
2021-11-23 16:40:024471browse

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 )

The example is as follows:

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" src="/jquery/jquery.js"></script>
</head>
<body>
     
<pre id="original">

<script>
$(function () { 
    var str = "         前面有空格  后面有空格         ";
    $( "#original" ).html( "Original String: &#39;" + str + "&#39;" );
    $( "#trimmed" ).html( "$.trim()&#39;ed: &#39;" + $.trim(str) + "&#39;" );
})
</script>
  

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!

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