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

Source: Internet
Author: User
Tags rtrim

Method 1: Regular Expression replacement recommendation
In my opinion, the best method is to use regular expressions, which is the core principle.
Below is the original code
Copy codeThe 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 codeThe 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 and achieves different implementation effects by passing different parameters.
<HTML> <HEAD> <TITLE> JavaScript Trim Function </TITLE> </HEAD> <BODY> remove spaces from strings in JavaScript (custom ): <br> </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.