Home > Web Front-end > JS Tutorial > body text

JavaScript trim Three ways to remove spaces from strings (with detailed code explanation)_javascript skills

WBOY
Release: 2016-05-16 18:26:10
Original
1192 people have browsed it
Method 1: Recommended regular replacement
I think this is the best method. It uses regular expressions, which is the core principle.
The following is the original code
Copy code The code is as follows:



Let’s do it Take a look at "What does /s mean" in JS script?
s matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv].
Please remember that it is lowercase s
Secondly, this method uses the JavaScript prototype attribute
In fact, you can also use a function to implement it without using this attribute. But it is more convenient to use this way.
Let's take a look at how this attribute is used.
Returns a reference to the prototype of the object type.
objectName.prototype
objectName parameter is the name of the object.
Description
Use the prototype property to provide a basic set of functionality for an object's class. New instances of an object "inherit" the operations assigned to the object's prototype.
For example, add a method to an Array object that returns the value of the largest element in the array. To accomplish this, declare the function, add it to 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 this code is executed, y holds the maximum value in array x, or 6.
All JScript internal objects have a read-only prototype property. You can add functionality to a prototype as in this example, but the object cannot be assigned a different prototype. However, user-defined objects can be assigned to new prototypes.
The list of methods and properties for each internal object in this language reference indicates which ones are part of the object's prototype and which ones are not.
Method 2:
Since the method of use is simple, I will not give an example here.
Copy code The 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 three:
This method writes functions together and achieves different effects by passing different parameters

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template