INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...Your site was well structured and I found what I was looking for in about 2 minutes. I am looking forward to participating with you in the future..."
Geography
Where in the world do Tek-Tips members come from?
|
Strings
|
trim(), startsWith() and endsWith() in JavaScript?
Posted: 17 Apr 07 (Edited 17 Apr 07)
|
All programmers who use JavaScript at some stage get frustrated searching for some standard functions that are simply not built into JavaScript! Also if you manage to write your own version for it, the call isn't as neat as the standard ones.
eg. calling the in-built substr() function would look something like myVar.substr(x, y) but if you wrote your own version (may be function substr2(str, x, y)) you'll have to call it as
CODEmyVar = substr2(myVar, x, y); //Your function myVar = myVar.substr(x, y); //built-in function Not so elegant is it? Well, here is a way you could BIND your functions to the data type and call them like in-built functions… here's how:
Trim function, trims all leading and trailing spaces:
CODEString.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))} startsWith to check if a string starts with a particular character sequecnce:
CODEString.prototype.startsWith = function(str) {return (this.match("^"+str)==str)} endsWith to check if a string ends with a particular character sequecnce:CODEString.prototype.endsWith = function(str) {return (this.match(str+"$")==str)} All these functions once loaded will behave as built-in JavaScript functions. Here are few examples:
CODEvar myStr = “ Earth is a beautiful planet ”; var myStr2 = myStr.trim(); //==“Earth is a beautiful planet”;
if (myStr2.startsWith(“Earth”)) // returns TRUE
if (myStr2.endsWith(“planet”)) // returns TRUE
if (myStr.startsWith(“Earth”)) // returns FALSE due to the leading spaces…
if (myStr.endsWith(“planet”)) // returns FALSE due to trailing spaces… Summary: Any function you think will be needed on a data type level can be implemented in this way… you don’t always need to use regular expressions for this but it does make it really compact.
Let me know if this was useful!![[thumbsup2] thumbsup2](http://www.tipmaster.com/images/thumbsup2.gif)
Enjoy scripting…![[noevil] noevil](http://www.tipmaster.com/images/noevil.gif) Jay
|
Back to Javascript FAQ Index
Back to Javascript Forum |
|
 |
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close