jQuery Data Type How to - Trim a string








Question

We would like to know how to trim a string.

Answer


<!--from   ww w .j  a  va 2 s. co m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
               var str = "     a    ";
               console.log("'" + str + "'");
               str = jQuery.trim(str);
               console.log("'" + str + "'");

        });
    </script>
  </head>
</html>

The code above is rendered as follows: