JavaScript Trim Function
I know my audience doesn’t care much for Web Development content, but this was a fascinating article about the various methods of trimming the white space from a text string.
See, most scripting languages have some kind of “trim()” function that you pass a string into. It removes extra spaces and return characters and line-feed characters and tabs and so forth. However, JavaScript doesn’t have one of these functions. In my research to determine the best method for constructing one, I ran across the aforementioned article. In it, Steve Levithan describes which browsers implemented which technique better, and why.
THE WINNER?
A function called trim11.
function trim11 (str) {
str = str.replace(/^\\s+/, '');
for (var i = str.length - 1; i > 0; i--) {
if (/\\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
About this entry
You’re currently reading “JavaScript Trim Function,” an entry on Now Direction
- Published:
- 12.12.07 / 5pm
- Category:
- Web Design
2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]