jQuery.trim()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Examples

Remove whitespace from the beginning and end of a string

$(function () { var str = " lots of spaces before and after "; $( "#original" ).html( "Original String: '" + str + "'" ); $( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" ); })

Try it Yourself »

Definition and Usage

The $.trim() function is used to remove whitespace from both ends of a string.

The $.trim() function is deprecated in jQuery 3.5 and above; you can use the native JavaScriptString.prototype.triminstead.

Note:The $.trim() function removes all newlines, spaces (including consecutive spaces), and tabs from the beginning and end of a string. If these whitespace characters are in the middle of the string, they are preserved and will not be removed.


Syntax

$.trim( str )

Parameter Description
str String type A string whose whitespace at both ends needs to be removed.


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions