Today I will show you how to make TRIM Function in Java Script .
From this tutorial you can easily write RTRIM and LTRIM function.
This Function is very useful you can also make you own TRIM function to filtering user input, Preventing Basic SQL Injection and verify or correct user input.
The basic purpose of TRIM function is remove white space from string.
Below is set of JavaScript functions TRIM or remove white space or specified character from string.These function can be stand-alone or attached as methods of the String object. In this we use elegant regular expression rather tutorial rather to use clumsy loops.
Attached as methods of the String object:
Stand-alone methods Example:
From this tutorial you can easily write RTRIM and LTRIM function.
This Function is very useful you can also make you own TRIM function to filtering user input, Preventing Basic SQL Injection and verify or correct user input.
The basic purpose of TRIM function is remove white space from string.
Below is set of JavaScript functions TRIM or remove white space or specified character from string.These function can be stand-alone or attached as methods of the String object. In this we use elegant regular expression rather tutorial rather to use clumsy loops.
Attached as methods of the String object:
/* ***************************** Remove White spaces by : http://visualstudiolearn.blogspot.com/ tpic : Attached as methods of the String object use to uncommit code to check output. *****************************/ String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); } String.prototype.ltrim = function() { return this.replace(/^\s+/,""); } String.prototype.rtrim = function() { return this.replace(/\s+$/,""); } var myString = " hello my name is "; alert("*"+myString.trim()+"*"); /*alert("*"+myString.ltrim()+"*"); alert("*"+myString.rtrim()+"*");*/ /* ***************************** Remove specified Character by http://visualstudiolearn.blogspot.com/ *****************************/ String.prototype.rtrim = function(s) { return this.replace(new RegExp(s + "*$"),''); }; String.prototype.ltrim = function(s) { return this.replace(new RegExp("^"+s ),''); }; String.prototype.trim = function(s) { var regex = new RegExp(s, 'g'); return this.replace(regex,''); }; var s1 = "~this is a test~"; var s = s1.trim('~'); alert(s); /* ***************************** More Detail and Tutorial Visit http://visualstudiolearn.blogspot.com/ *****************************/Test Now
Stand-alone methods Example:
/* ***************************** Remove White spaces by : http://visualstudiolearn.blogspot.com/ tpic : Attached as methods of the String object use to uncommit code to check output. *****************************/ function trim(stringToTrim) { return stringToTrim.replace(/^\s+|\s+$/g,""); } function ltrim(stringToTrim) { return stringToTrim.replace(/^\s+/,""); } function rtrim(stringToTrim) { return stringToTrim.replace(/\s+$/,""); } var myString = " hello my name is "; alert("*"+trim(myString)+"*"); alert("*"+ltrim(myString)+"*"); alert("*"+rtrim(myString)+"*"); /* ***************************** Remove specified Character by http://visualstudiolearn.blogspot.com/ *****************************/ function trim(stringToTrim, Char) { var regex = new RegExp(Char, 'g'); return stringToTrim.replace(regex,''); } function ltrim(stringToTrim, Char) { return stringToTrim.replace(new RegExp("^"+Char ),''); } function rtrim(stringToTrim, Char) { return stringToTrim.replace(new RegExp(Char + "*$"),''); } var s1 = "~this is a test~"; alert(trim(s1,'~')); alert(ltrim(s1,'~')); alert(rtrim(s1,'~')); /* ***************************** More Detail and Tutorial Visit http://visualstudiolearn.blogspot.com/ *****************************/Test Now
0 comments:
Post a Comment