trim() is a very common and very useful function in almost every programming language. basically, it's used to trim or suppress all white spaces at the begging and the end of a given string. e.g: myString = " hello world "; myString.trim() returns: "hello world".
why is this so useful? well, there are many reasons, perhaps the most important is to ensure in the string, that a formal null value is actually a non formal null value. how so? imagine this:
myString = "": the value of myString is a formal null value due to it has no reference to any reserved space on memory.
myString = "hello world": the value of myString is 'hello world';
myString = " ": ??? the value is null?? or not null?;
well it obviously isn't null, due to the character " " is actually A CHARACTER.
but for real purposes, it does seems like a null value, since it has no relevant data. that is a non formal null value.
we use non formal null value in most of our forms: login, register, contact, etc. and we do need to ensure that a non formal null == a formal null.
so how to solve this:
1.- using a for or any kind of cycle to go trough the whole string and check for any white spaces, comparing at the end if whites paces==string length, if so, it means all characters are white spaces therefore its a non formal null.
2.- other way is to check if at least one character of the string is different of white space, else, it's a non formal null.
using any of this two methods isn't quite appropriate and will present a problem:
" hello world " is neither a non formal neither a formal null, how ever, we'll have problems handling this value in case of passwords (e.g. getting max length), or name searches or any sorting methods since it has white spaces at the beginning of the string.
so, trying to implement all methods to solve both problems will take us long time, effort and it'll result an unpractical solution.
so here goes the best solution for solving those problems: using trim(), by suppressing white spaces at the beginning and at the end, we solve both problems at once, e.g.:
myString = " "; myString.trim() returns null; (non formal null solved!)
myString = " hello world "; myString.trim() returns "hello world" (there are no white spaces that provoke future problems like sorting).
that is just great isn't? so go on and try it on any language or script! just type yourString.trim()!.
oh, wait, it just happened you're using javascript? guess what... it has no trim() function implemented on the String object!. sorry folks =(.
but hey, take off those long faces, there is a way to implement it yourself and that is using the prototype of String to add new methods. there are two ways to do this:
1.- using a for or any kind of cycle.
2.- using regular expressions.
I'll let yourself figure out the first one since is quite boring for me to do it =P.
and I'll post the second bellow since is the best and most optimum way and it takes just a simple line!.
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, '');
}I'll make a detailed explanation of the code on my next post, so stay tuned.
mean wile enjoy it =P.