Two definitions of JavaScript trim () Functions

Source: Internet
Author: User
Tags rtrim

 

1 First:
2 String. Prototype. ltrim =   Function () {
3Return This. Replace (/^ \ S */,"");
4} ; // Remove the left space;
5 String. Prototype. rtrim =   Function () {
6Return This. Replace (/\ S * $/,"");
7} ; // Remove spaces on the right;
8 String. Prototype. Trim =   Function () {
9Return This. Ltrim (). rtrim ();
10} ; // Remember that each sentence has a semicolon
11
12 Second:
13 Function Ltrim (STR) {
14 If (Str. charat ( 0 ) = "" ) {
15Str=Str. Slice (1);
16Str. ltrim ();
17}
18 Return STR;
19 }
20
21 Function Rtrim (STR) {
22 If (Str. charat (Str. Length - 1 ) = "" ) {
23Str=Str. Slice (0, Str. Length-1);
24Str. rtrim ();
25}
26 Return STR;
27 }
28
29 Function Trim (STR) {
30ReturnRtrim (ltrim (STR ));
31}

AboveCodeFor beginners to learn. Is the most basic function in JavaScript scripts.
The first kind of self-perception is better, and the prototype is adopted. Hope you can express your comments

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.