Jump to content

String.trim()


PrateekSaxena

Recommended Posts

hi,Well a few minutes back I found out that the String object does not have a trim method in it that would remover the trailing and starting spaces. So is there a method for the String Object that is similar to trim()?? If not please suggest what I can do get the same functionality as trim()??

Link to comment
Share on other sites

place this in an external js file and include it at the start of your page.

//extending string methods	String.prototype.trimLeft = new Function("return this.replace(/^\\s+/,'')")	String.prototype.trimRight = new Function("return this.replace(/\\s+$/,'')")	String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')")

This will add the three methods to all strings. I believe this code was compliments of jesh.

Link to comment
Share on other sites

I am sure that this works, but I don't want to use it till you explain me how it is actually worked! I mean don't write the whole thing, give me a link that will be able to explain what is happening!

Link to comment
Share on other sites

Any other more detailed documents??

Link to comment
Share on other sites

Thanks man! This is really useful!

Link to comment
Share on other sites

But sometimes there is not other way to do something, or is there??

Link to comment
Share on other sites

I believe this code was compliments of jesh.
:)
Yeah me too.... Never tried to write one on my own, always looked to find something online incase there is no other way other than using regex...
Although I found that they had a very steep learning curve, Regular Expressions aren't really all that difficult once you understand them. And they are ridiculously powerful.
Link to comment
Share on other sites

So it considered good to use them right?? they aren't like global variables??

Link to comment
Share on other sites

:) Although I found that they had a very steep learning curve, Regular Expressions aren't really all that difficult once you understand them. And they are ridiculously powerful.
I agree they are very powerful. I don't use them enough to retain what I learn each time and I just can't bring myself to take the time to learn. :)
Link to comment
Share on other sites

So they aren't like global variables!right?

Link to comment
Share on other sites

Alright then it is ok. Lately I have learning nice things about JS, stuff like

var allow = (age<18) ? no:yes;

or

for(var i in a){}

Are there any other thing like this??

Link to comment
Share on other sites

Yeah, they are search patterns.

var string = "  this is a test";string = string.replace(/^\\s+/g, '');

Does the same thing as:

var string = "  this is a test";while(string.substring(0,1) == " "){	string = string.substr(1);}

As for other things you can do with javascript. Take a look at bit shifting (the << and >> operators). I don't fully understand it but I've seen some cool things done with it.Also, a fun one I've learned recently is that you can pass a reference to a function as a parameter to another function and then call that function on the inside:

function a(string){	alert(string);}function b(string, func){	func(string);}b("What's up?!", a);

Link to comment
Share on other sites

I think you will really like this http://developer.mozilla.org/en/docs/A_re-...n_to_JavaScriptRead the whole thing, I learned stuff that I never knew before!!!

Link to comment
Share on other sites

I think you will really like this http://developer.mozilla.org/en/docs/A_re-...n_to_JavaScriptRead the whole thing, I learned stuff that I never knew before!!!
Great link. Within the first couple of sections I learned this:
If you want to convert a binary number to an integer, just change the base:> parseInt("11", 2)3
Which means this:
var decimal = parseInt("FF8C00", 16)

would have come in real handy for me when I was writing some color functions...

Link to comment
Share on other sites

The object oriented part is very nice too! http://www.google.com/coop/cse?cx=00068528...8%3Axvj-i48acfm

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...