The String object's trim() method
The String object's trim()
method removes whitespace characters from both ends of a string. Whitespace includes spaces, tabs, newlines, and other space-like characters.
Prerequisites
This tutorial assumes that you're familiar with:
Syntaxstring.trim()
Parameters
string.trim()
Not applicable. The trim()
method doesn't accept any parameters.
Return value
Returns a new string with whitespace removed from both ends. The original string is not modified.
ExamplesExample 1: Basic trimming
In the code below, the function trimEx1()
demonstrates basic whitespace removal.
function trimEx1() {
let str1 = " Hello World! ";
let str2 = "\n\tHello Apps Script\n\t";
// Remove spaces from both ends
Logger.log("'" + str1.trim() + "'");
// Remove tabs and newlines from both ends
Logger.log("'" + str2.trim() + "'");
}
Output
'Hello World!'
'Hello Apps Script'
Example 2: Handling different types of whitespace
In the code below, the function trimEx2()
shows how trim()
handles various whitespace characters.
function trimEx2() {
// Space, tab, newline, carriage return
let str = " \t \n \r Example String \n \t \r ";
Logger.log("Original length: " + str.length);
let trimmed = str.trim();
Logger.log("Trimmed length: " + trimmed.length);
Logger.log("Trimmed string: '" + trimmed + "'");
}
Output
Original length: 28
Trimmed length: 14
Trimmed string: 'Example String'
Example 3: Real-world applications
In the code below, the function trimEx3()
demonstrates common use cases for the trim()
method.
function trimEx3() {
// Clean user input
let userInput = " john.doe@example.com ";
let email = userInput.trim();
Logger.log("Cleaned email: " + email);
// Split and clean CSV data
let csvLine = " Item 1 , Item 2 , Item 3 ";
let cleanItems = csvLine.split(',').map(item => item.trim());
Logger.log(cleanItems);
// Check for empty string after trimming
let whitespaceOnly = " \n\t ";
Logger.log("Is empty after trim: " + (whitespaceOnly.trim() === ""));
}
Output
Cleaned email: john.doe@example.com
[Item 1, Item 2, Item 3]
Is empty after trim: true
Example 4: Demonstrating immutability
In the code below, the function trimEx4()
shows that trim()
returns a new string without modifying the original.
function trimEx4() {
let originalStr = " Padded String ";
// Create new string with trimming
let trimmedStr = originalStr.trim();
// Show that original string is unchanged
Logger.log("Original string: '" + originalStr + "'");
Logger.log("Trimmed string: '" + trimmedStr + "'");
// Length comparison
Logger.log("Original length: " + originalStr.length);
Logger.log("Trimmed length: " + trimmedStr.length);
}
Output
Original string: ' Padded String '
Trimmed string: 'Padded String'
Original length: 19
Trimmed length: 13
Common Use Cases
The trim()
method is useful for cleaning and validating text data. Here are common scenarios where you'll find it useful:
Form input cleaning: Remove unnecessary whitespace from user input before processing or storing it.
Data validation: Ensure strings aren't just whitespace when requiring non-empty input.
CSV processing: Clean individual fields when parsing comma-separated values.
Text comparison: Normalize strings by removing whitespace before comparing them.
Username/password validation: Remove accidental spaces from login credentials.
Search functionality: Clean search terms to improve matching accuracy.
Related Methods
trimStart()
(ortrimLeft()
): Removes whitespace only from the beginning of a stringtrimEnd()
(ortrimRight()
): Removes whitespace only from the end of a string
Note
While trim()
handles all whitespace characters, it only removes them from the ends of the string. Whitespace between words remains unchanged.
Conclusion
In this tutorial, you learned how to use the trim()
method to remove whitespace from strings in Apps Script. This method is useful for data cleaning and validation tasks by removing unnecessary whitespace from strings. Remember that trim()
returns a new string and doesn't modify the original string.