Method One:
Personally think 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.
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
Remove all spaces in the string var inputcontent = TxtObj.value.replace (/\s/g, "");
<script language= "JavaScript" >
<!--
Source: Online Collection
Made by Yaosansi 2005-12-02
For more visit http://www.yaosansi.com
Trim (), Ltrim (), RTrim ()
-->
</SCRIPT>
Let's take a look at what "s/s represents in the JS script.
\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 of the simplicity of the method, there is no example here.
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 functions together and achieves different implementations by passing the parameters differently
<HTML>
<HEAD>
<title>javascript Trim function</title>
<script language=javascript>
1:
2: <!--
3://****************************************************************
4://description:sinputstring for input string, itype for type, respectively
5://0-remove before and after space; 1-Go leading blanks; 2-Go to the trailing blanks
6://****************************************************************
7:function Ctrim (Sinputstring,itype)
8: {
9:var stmpstr = '
10:var i =-1