JavaScript Trim three ways to remove string spaces (with code detailed) _javascript tips

Source: Internet
Author: User
Tags rtrim
method One: Regular replacement recommendation
Personally think the best way. Using regular expressions, this is the core principle.
The following is the original code
Copy Code code 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>

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
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.
Method Two:
Because of the simplicity of the method, there is no example here.
Copy Code code as follows:

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 functions together and achieves different implementations by passing the parameters differently
<HTML> <HEAD> <title>javascript Trim function</title> <script language=javascript> ;! --//****************************************************************//description:sinputstring for input string, itype for type, respectively//0-Remove before and after the space; 1-Go leading blanks; 2-Go tail space//**************************************************************** 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.len Gth 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>") documen T.write ("R2 = '" + sR2 + "' <br>")//--> </SCRIPT> </BODY> </HTML>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.