Implementing String.prototype.trim in JavaScript
The trim() method is a built-in JavaScript function that removes whitespace from both ends of a string. This challenge asks you to implement this functionality yourself, providing a deeper understanding of string manipulation and algorithm design. Implementing trim() is a common task and understanding how it works is a valuable skill.
Problem Description
Your task is to implement a trim function that extends the JavaScript String.prototype with a trim method. This method should remove all leading and trailing whitespace characters from a string. Whitespace characters include spaces, tabs (\t), newlines (\n), carriage returns (\r), and form feeds (\f). The method should return a new string with the whitespace removed, leaving the original string unchanged.
Key Requirements:
- Extend
String.prototype: The solution must add atrimmethod directly to theStringprototype, making it available for all strings. - Remove Leading and Trailing Whitespace: The method must remove whitespace from the beginning and end of the string.
- Whitespace Definition: Whitespace characters are defined as spaces, tabs (
\t), newlines (\n), carriage returns (\r), and form feeds (\f). - Immutability: The original string should not be modified. The method must return a new string.
- Handle Empty Strings: The method should correctly handle empty strings.
- Handle Strings with Only Whitespace: The method should correctly handle strings containing only whitespace characters.
Examples
Example 1:
Input: " Hello, world! "
Output: "Hello, world!"
Explanation: Leading and trailing spaces are removed.
Example 2:
Input: " \tHello\n, world!\r\f "
Output: "Hello, world!"
Explanation: All leading and trailing whitespace characters (space, tab, newline, carriage return, form feed) are removed.
Example 3:
Input: ""
Output: ""
Explanation: An empty string remains an empty string.
Example 4:
Input: " "
Output: ""
Explanation: A string containing only whitespace becomes an empty string.
Example 5:
Input: "Hello, world!"
Output: "Hello, world!"
Explanation: A string with no leading or trailing whitespace remains unchanged.
Constraints
- The input string will be a standard JavaScript string.
- The method must be implemented efficiently, avoiding unnecessary iterations or string allocations. While performance isn't the primary focus, avoid grossly inefficient solutions.
- The solution must be compatible with all modern JavaScript environments.
Notes
- Consider using regular expressions for a concise solution, but be mindful of potential performance implications.
- Think about how to efficiently find the first non-whitespace character from the beginning and the last non-whitespace character from the end of the string.
- Remember that you are modifying the
String.prototype, so be careful not to introduce any conflicts with existing methods. UsingString.prototype.trim = ...is the correct way to extend the prototype. - Test your solution thoroughly with various edge cases, including empty strings, strings with only whitespace, and strings with mixed whitespace and non-whitespace characters.