Javascript trim three methods for removing string spaces (detailed code)

Source: Internet
Author: User

Method 1: Regular Expression replacement recommendation
In my opinion, the best method is to use regular expressions, which is the core principle.
Below is Code Original Copy code The Code is as follows: <script language = "JavaScript">
<! --
// Source: online collection
// For more visit http://www.jb51.net
// Trim (), ltrim (), rtrim ()
String. Prototype. Trim = function ()
{
Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");
}
String. Prototype. ltrim = function ()
{
Return this. Replace (/(^ \ s *)/g ,"");
}
String. Prototype. rtrim = function ()
{
Return this. Replace (/(\ s * $)/g ,"");
}
// -->
</SCRIPT>

Next let's take a look at "/s" in the JS script"
\ S matches any blank characters, including spaces, tabs, and page breaks. It is equivalent to [\ f \ n \ r \ t \ v].
Please note that it is lowercase s
Secondly, this method uses the prototype attribute of JavaScript.
In fact, you can use functions without using this attribute, but it is more convenient to use it later.
Next let's take a look at how this attribute is used.
Returns a reference to an object type prototype.
Objectname. Prototype
The objectname parameter is the object name.
Description
The prototype attribute provides a set of basic functions of the object class. The new instance of the object "inherits" the operation that is granted to the object prototype.
For example, you want to add a method to the array object to return the maximum element value in the array. To accomplish this, declare the function, add it to array. prototype, and use it.
Function array_max () {var I, max = This [0]; for (I = 1; I <this. length; I ++) {If (max <this [I]) max = This [I];} return Max;} array. prototype. max = array_max; var x = new array (1, 2, 3, 4, 5, 6); var y = x. max ();
After the code is executed, y saves the maximum value in array X, or 6.
All internal JScript objects have the read-only prototype attribute. You can add features for the prototype as in this example, but the object cannot be assigned different prototypes. However, user-defined objects can be assigned to new prototypes.
The method and attribute list of each internal object in this language reference specifies which parts of the object prototype and which parts are not.
Method 2:
The usage is simple, so we will not give an example here. Copy code The Code is as follows: // JavaScript space Removal Function
Function ltrim (STR) {// remove the leading space of the string
VaR I;
For (I = 0; I if (Str. charat (I )! = "" & Str. charat (I )! = "") Break;
}
STR = Str. substring (I, str. Length );
Return STR;
}
Function rtrim (STR ){
VaR I;
For (I = Str. Length-1; I> = 0; I --){
If (Str. charat (I )! = "" & Str. charat (I )! = "") Break;
}
STR = Str. substring (0, I + 1 );
Return STR;
}
Function trim (STR ){
Return ltrim (rtrim (STR ));
}

method 3:
This method writes functions together, different implementation effects are achieved by passing different parameters <br/> remove the space function (custom) in javascript ): <br/>

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.