Three ways for JavaScript to remove spaces

Source: Internet
Author: User
Tags rtrim trim

Method One: The best way. Using regular expressions, this is the core principle.
Second, this method uses the prototype properties of JavaScript

In fact, you do not use this property can be implemented using the function. But this is easier to use.
Let's take a look at how this property is used.

Reference content

Returns a reference to the object type prototype.

Objectname.prototype
The objectname parameter is the name of the object.

Description
Use the prototype property to provide a set of basic functions for the object's class. The operation of the new instance of the object, "inherit", gives the object a prototype.

For example, to add a method that returns the maximum element value in an array for the array object. To do this, declare the function, add it to the 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 executes, Y saves the maximum value in the array x, or says 6.

All JScript internal objects have read-only prototype properties. You can add functionality to the prototype as in this example, but the object cannot be given a different prototype. However, user-defined objects can be assigned to a new prototype.

The list of methods and attributes for each internal object in the language reference indicates which parts of the object's prototype and which are not.

The following is the original code

Program code

<script language= "JavaScript" >
<!--
Source: Online Collection
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>

Use the method to see the following code

HTML code
-----------------------------------------------------
<script language= "JavaScript" >
<!--
Source: Online Collection
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>
<input type= "text" value= "both before and after the space" id= "spaces" >
<input type= "button" value= "go before and after the space" onclick= "javascript:document.getElementById (' spaces '). Value="/ document.getElementById (' space '). Value. Trim ();d Ocument.getelementbyid (' Space '). Select (); "" >
<input type= "button" value= "go to the front space" onclick= "javascript:document.getElementById (' spaces '). Value="/ document.getElementById (' space '). Value. LTrim ();d Ocument.getelementbyid (' Space '). Select (); "" >
<input type= "button" value= "to go after the space" onclick= "javascript:document.getElementById (' spaces '). Value="/ document.getElementById (' space '). Value. RTrim ();d Ocument.getelementbyid (' Space '). Select (); "" >
<input type= "button" value= "Restore" onclick= "javascript:document.getElementById (' space '). value= ' All spaces '; >
<a href= "http://www.webjx.com" target= "_blank" > Access webjx.com</a>
---------------------------------------------------

Let's take a look at what "s/s represents in the JS script.

Reference content

\s matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

Please bear in mind that the lowercase s

Method Two: Because the use method is simple, therefore here does not cite the example.

Reference content

JavaScript Go to space function
function LTrim (str) {//Remove the header space of the string
var i;
For (I=0;i if (Str.charat (i)!= "" &&str.charat (i)!= "") a 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 Three: This method writes the function together, passes the parameter different to achieve the different realization effect

Reference content

<HTML>
<HEAD>
<title>javascript Trim function</title>
<script language=javascript>
<!--
//****************************************************************
Description:sinputstring is the input string, Itype is the type, respectively
0-Remove before and after space; 1-Go leading blanks; 2-Go to the trailing blanks
//****************************************************************
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>
The string in JavaScript removes the space function (custom):<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>

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.