PrateekSaxena Posted March 16, 2007 Share Posted March 16, 2007 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 More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 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 More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 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 More sharing options...
pulpfiction Posted March 16, 2007 Share Posted March 16, 2007 Here's the link http://www.somacon.com/p355.phpbut no explanation about the regex in there.... as of I know, White space in regular expressions is described as \s. The beginning of the string is matched by ^ and the end is matched by $. Link to comment Share on other sites More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 Any other more detailed documents?? Link to comment Share on other sites More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 http://www.regular-expressions.info/ Link to comment Share on other sites More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 Thanks man! This is really useful! Link to comment Share on other sites More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 glad you like it...I hate reg exp...I avoid them if at al possible Link to comment Share on other sites More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 But sometimes there is not other way to do something, or is there?? Link to comment Share on other sites More sharing options...
pulpfiction Posted March 16, 2007 Share Posted March 16, 2007 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... Link to comment Share on other sites More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 I can write basic regexp but I usually have to go looking for help Link to comment Share on other sites More sharing options...
pulpfiction Posted March 16, 2007 Share Posted March 16, 2007 But sometimes there is not other way to do something, or is there??here's another way,http://www.aspdev.org/articles/javascript-trim/ Link to comment Share on other sites More sharing options...
jesh Posted March 16, 2007 Share Posted March 16, 2007 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 More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 So it considered good to use them right?? they aren't like global variables?? Link to comment Share on other sites More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 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 More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 So they aren't like global variables!right? Link to comment Share on other sites More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 nope, they are search patterns.The code I gave you extends the native javascript String object. The 3 extended functions use regexp to find white space and strip it. Link to comment Share on other sites More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 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 More sharing options...
aspnetguy Posted March 16, 2007 Share Posted March 16, 2007 if you want to have some fun research Memory leaks and Closures in IE Link to comment Share on other sites More sharing options...
jesh Posted March 16, 2007 Share Posted March 16, 2007 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 More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 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 More sharing options...
jesh Posted March 16, 2007 Share Posted March 16, 2007 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)3Which 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 More sharing options...
PrateekSaxena Posted March 16, 2007 Author Share Posted March 16, 2007 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now