Several Methods to remove spaces in JavaScript

Source: Internet
Author: User

Method 1:
In my opinion, the best method is to use regular expressions, which is the core principle.
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.
Copy codeThe Code is as follows:
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.
Below is the original code
 Copy codeThe Code is as follows:
<Script language = "JavaScript">
<! --
// Source: online collection
// Made by yaosansi 2005-12-02
// For more visit http://www.yaosansi.com
// 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

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.
<HEAD> <TITLE> JavaScript Trim Function </TITLE> </HEAD> <BODY> remove spaces (custom) from strings in JavaScript: </BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
4. This method is also good.
Copy codeThe Code is as follows:
// LTrim () removes the space on the left of the string
Function lTrim (str)
{
If (str. charAt (0) = "")
{
// If the first character on the left of the string is a space
Str = str. slice (1); // remove spaces from the string
// This sentence can also be changed to str = str. substring (1, str. length );
Str = lTrim (str); // recursive call
}
Return str;
}
// Remove the space on the right of the string from rTrim ()
Function rTrim (str)
{
Var iLength;
ILength = str. length;
If (str. charAt (iLength-1) = "")
{
// If the first character on the right of the string is a space
Str = str. slice (0, iLength-1); // remove spaces from the string
// This sentence can also be changed to str = str. substring (0, iLength-1 );
Str = rTrim (str); // recursive call
}
Return str;
}
// Trim () removes spaces on both sides of the string
Function trim (str)
{
Return lTrim (rTrim (str ));
}

5,Copy codeThe Code is as follows:
Function Trim (sText)
{
Return sText. replace (new RegExp ("(^ [\ s] *) | ([\ s] * $)", "g "),"");
}

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.