JavaScript -- add the trim, ltrim, and rtrim methods to the String object in JavascriptUsing the prototype attribute of each Object in Javascript, we can add our own method to the built-in objects in Javascript.
And attributes. Here we will use
// Function: trim (), ltrim (), and rtrim () functions of JavaScript// Source: http://jorkin.reallydo.com/article.asp? Id = 460String. Prototype. Trim = function (){Return this. Replace (/(^ \ s *) | (\ s * $)/g ,"");}
String. Prototype. ltrim =
Removing the spaces at each end of the string is a very common method of string processing, and unfortunately JavaScript does not have these three methods, only we have customized:
Step 1th, add members to string
Copy Code code as follows:
Using the prototype attribute of each Object in Javascript, we can add our own methods and attributes to the built-in objects in Javascript. In the following example, we use this attribute to add three methods to the String object: Trim, LTrim, and
Js remove space instance Trim (), LTrim (), RTrim () need friends can come to refer to, hope to help everyone
1. Remove string spaces in js// Remove the left space;
Function ltrim (s)
...{
Return s. replace (/(^ s *)/g ,"");}// Remove the
Today, javascript is used to remove the spaces at both ends of a string in a text box. At first, I thought there were trim, ltrim, and rtrim functions (asp had these three functions mixed up). I found the result for half a day, not found. Finally,
In the following example, we use this attribute to add three methods to the String object: Trim, LTrim, and RTrim (same as the function of the same name in VbScript)
Copy codeThe Code is as follows: String. prototype. Trim = function ()
{
Return
In the following example, we use this attribute to add three methods to the String object: Trim, LTrim, and RTrim (same as the function of the same name in VbScript)Copy codeThe Code is as follows: String. prototype. Trim = function (){Return this.
Using the prototype property of each object in JavaScript, we can add our own methods and properties to the built-in objects in JavaScript.Here's how we use this property to add three methods to a String object: Trim,ltrim,rtrim (acting like a
js| function adds a Trim,ltrim,rtrim method to a String object in JavaScript
Using the prototype property of each object in JavaScript, we can add our own methods and properties to the built-in objects in JavaScript.
Here's how we use this
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.