Remove spaces at both ends of the JavaScript string, and press Enter.

Source: Internet
Author: User

Many languages have specialized functions or methods to delete spaces at the beginning and end of a string, such as chomp of Perl and strip of Python. Then JavaScript does not seem to have similar functions, which are not mentioned in some manuals. In fact, the trim function has been added to JavaScript 1.8.1 to implement this function. Most browsers also provide support. The following provides a simple implementation.

Method to add a String object:

  1. If (typeof (String. prototype. trim) = "undefined ")
  2. {
  3. String. prototype. trim = function ()
  4. {
  5. Return String (this). replace (/^ \ s + | \ s + $/g ,'');
  6. };
  7. }


  8. "Dog". trim () = "dog"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.