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