Three methods for JavaScript to remove spaces (regular expressions, parameter passing functions, and trim)

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
Program codeCopy 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.Copy codeThe Code is as follows: <HTML>
<HEAD>
<TITLE> JavaScript Trim Function </TITLE>
<Script language = javascript>
<! --
//************************************** **************************
// Description: sInputString is the input string, and iType is the type, which is
// 0-Remove leading and trailing spaces; 1-Remove leading spaces; 2-Remove trailing Spaces
//************************************** **************************
Function cTrim (sInputString, iType)
{
Var sTmpStr =''
Var I =-1

If (iType = 0 | iType = 1)
{
While (sTmpStr = '')
{
++ I
STmpStr = sInputString. substr (I, 1)
}
SInputString = sInputString. substring (I)
}

If (iType = 0 | iType = 2)
{
STmpStr =''
I = sInputString. length
While (sTmpStr = '')
{
-- I
STmpStr = sInputString. substr (I, 1)
}
SInputString = sInputString. substring (0, I + 1)
}
Return sInputString
}
// -->
</SCRIPT>

</HEAD>

<BODY>
Remove space functions (custom) from strings in JavaScript: <br>

<Script language = javascript>
<! --
Var sR0 = cTrim ("T e s t", 0)
Var sR1 = cTrim ("T e s t", 1)
Var sR2 = cTrim ("T e s t", 2)
Document. write ("R0 = '" + sR0 + "' <br> ")
Document. write ("R1 = '" + sR1 + "' <br> ")
Document. write ("R2 = '" + sR2 + "' <br> ")
// -->
</SCRIPT>
</BODY>
</HTML>

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.