How to trim a string using jQuery?

Posted by Poster under jQuery category on | Views : 28006
To trim for extra white spaces before and after the string, you can use trim function of the jQuery.

<script type="text/javascript">
var v = $.trim(" There are extra spacces before and after this sentences. ");

This will trim the extra white spaces before and after the sentence and alert the result string.

Comments or Responses

Posted by: Virendradugar on: 4/28/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Poster,

Thanks for this useful code. There is one more advantage of using trim that you can check element is empty or not. See below post.

Virendra Dugar
Posted by: Virendradugar on: 4/9/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Well as you said that trim removes spaces from beginning and end of string. But what if you want to remove all the spaces within the string? Please read this article which explains how to remove all spaces from string using jQuery.

Login to post response