Email This Post
Print This Post
Trim Spaces
Thu, Nov 8, 2007 – 10:51 amSometimes you’ll have unnecessary spaces at the beginning or end of a character string that you wish to remove. For some unknown reason a trim space method or function was not included as part of the JavaScript language, even though this comes standard in most other programming languages.
Well, here are two different solutions for you. The first one uses straight forward “while loop” approach. Each character in the front of the string is checked. if it is a space then the string is changed to exclude it from the front. It does this until a non-space character is reached. The same is then done for the end of the string. Though in this case the string is searched from right to left.
First Solution
function trimSpace(sString) {
while (sString.substring(0,1) == ' ') {
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ') {
sString = sString.substring(0,sString.length-1);
}
return sString;
}
Example of how it would be used
X = ' Bob '; X = trimSpace(X); // // X is now equal to 'Bob' //
The Second Solution
This next solution is both simpler and yet trickier for those who are not used to Regular Expressions in JavaScript.
Regular expressions are a form of pattern matching that you can apply on textual content. Take for example the DOS wildcards ? and * which you can use when you’re searching for a file. That is a kind of very limited subset of RegExp. For instance, if you want to find all files beginning with “fn”, followed by 1 to 4 random characters, and ending with “ht.txt”, you can’t do that with the usual DOS wildcards. RegExp, on the other hand, could handle that and much more complicated patterns….more
The scope of this article is not to discuss Regular Expression syntax in detail, for that please follow the link posted above. I simply want to show how powerful they can be by demonstrating their use in removing leading and trailing spaces from a string.
function trimSpace(s) {
var SpcEnds = new RegExp("^\\s*|\\s*$", "g"); // Create Regular expression
s = s.replace(SpcEnds, ""); // Remove leading or trailing spaces.
return s;
}
It would be called and used in the same way as the first solution.
Conclusion
Which solution should you use?
Note: Regardless of the number of lines used, neither solution executes measurably faster than the other.
Solution 1, the traditional while loop approach, is easy to read and this will allow for most developers to understand it more readily. This in turn makes it easier to debug if issues arise.
But - More code does offer more complexity. It’s easy to understand, yet introduces more lines of code which can lead to some inflexibility.
Solution 2, the Regular Expression approach, has the advantage of offering even more advanced pattern matching functionality if later on you needed to expand this to remove embedded spaces as well, or replace the space with a different character.
But - this flexibility comes at the cost of clarity which in term can impact the maintainability of the code. It’s powerful, yet relatively obscure.
Which should you use? Well, that decision will be up to you.