Strings are one of the most basic structures in any development language. Developing with strings happens in common ways across languages. Sometimes languages don’t offer the common gamut of tools and leave the developer to implement their own methods. One of these cases occurs with a proper
String.Trim with EcmaScript5, which is a specialized version of a generic
strip, so that leaves developers to implement it themselves.
If you’ve written Python, you are most likely familiar with the Python
strip method. In Java,
trim does not have a native implementation. Rely instead on the trusty Apache Commons
StringUtils package for an equivalent
trim method, but also for the
strip method. PHP has the
trim method, but the method’s signature is the same as the Python
strip method, so it’s only a
trim method in case the browser is missing the native function. Other utility libraries, like LoDash and Underscore, however are devoid of
trim unless further extensions are included like Underscore.string, which also has
Stripping text is the removal of characters from both sides of a string that match the pattern given. Generally stripping text without telling it what characters to look for will cause whitespace characters except for newlines to be removed. If you are dealing with strings, especially text driven from user input,
strip will become something you find necessary for tasks like validation, matching, caching and tracking keywords.
Regardless, the generally more powerful
trim, but also fast to remove any character. Many implementations also implement
rstrip to target one side of the string or the other. I’ll leave that for the advanced user.
The first part of my
strip method is to validate if any characters were passed in. If nothing was passed through, then we assume a
trim was wanted, and if
trim is implemented by the browser, just use that method and do an early return. Otherwise, if
trim is not implemented, set the characters to an effective
trim of whitespace characters.
The next step of the function is to be a little proactive with the characters that we are allowing. Escaping characters that might break the RegExp is a required step.
Finally the function uses a RegExp to target the beginning and end of the string with the characters given and replaces them with nothing. It’s that simple, but powerful.
- Performance differences
- Polyfilling Trim methods