Javascript removes spaces on the left or right of the string

Source: Internet
Author: User
In a string, sometimes there is a space character on the left or right of the string. during development, we may need to remove the white space on the left or right. Js does not have trim functions like php. In this case, we have to write a js function to delete spaces in two string segments. This article introduces how to delete spaces between the left and right strings in JavaScript. The Code is as follows:

1. Delete spaces on the left of the string.

Function leftTrim (str) {if (str. charAt (0) = "") {str = str. slice (1); str = leftTrim (str);} return str ;}

2. Delete spaces on the right of the string

Function rightTrim (str) {if (str. length-1> = 0 & str. charAt (str. length-1) = "") {str = str. slice (0, str. length-1); str = rightTrim (str);} return str ;}

If you need colleagues to delete the spaces of the two strings, you can directly combine the two functions.

The js/html/php/css code in this article can be copied to this page for online debugging. You may wish to give it a try.

Http://www.manongjc.com/runcode

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.