ドキュメント

Utilities/jQuery.trim

From jQuery JavaScript Library

Jump to: navigation, search

« Back to Utilities

jQuery.trim( str )

Remove the whitespace from the beginning and end of a string.
Uses a regular expression to remove whitespace from the given string.
Arguments:
strString
The string to trim.
Examples:

Removes the two whitespaces at the start and at the end of the string.

    $("button").click(function () {
      var str = "     lots of spaces before and after     ";
      alert("'" + str + "'");

      str = jQuery.trim(str);
      alert("'" + str + "' - no longer");
    });

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  
  <script>
  $(document).ready(function(){
    
    $("button").click(function () {
      var str = "     lots of spaces before and after     ";
      alert("'" + str + "'");

      str = jQuery.trim(str);
      alert("'" + str + "' - no longer");
    });

  });
  </script>
  
</head>
<body>
  <button>Show Trim Example</button>
</body>
</html>

Removes the two whitespaces at the start and at the end of the string.

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

"hello, how are you?"

NameType