jQuery.trim()

jQuery.trim()

jQuery.trim( str )Returns: String

Description: Remove the whitespace from the beginning and end of a string.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

Examples:

Remove the white spaces at the start and at the end of the string.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.trim demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js" rel="external nofollow" ></script>
</head>
<body>
 
<pre id="original"></pre>
<pre id="trimmed"></pre>
 
<script>
var str = "         lots of spaces before and after         ";
$( "#original" ).html( "Original String: '" + str + "'" );
$( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
</script>
 
</body>
</html>

Demo:

Remove the white spaces at the start and at the end of the string.

$.trim("    hello, how are you?    ");

Result:

"hello, how are you?"

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/jQuery.trim

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部