Dromo WebinarLearn how Dromo can solve your data importing problems

Register now

Trimming Fields

Removing unnecessary characters, typically whitespace, from the start and end of a string.

Definition

Trimming fields refers to the process of removing unnecessary characters, typically whitespace, from the start and end of a string. This process is common when dealing with user-generated text data, which may include leading or trailing spaces, tabs, or other whitespace characters that were added inadvertently.

When trimming whitespace, it's important to consider that not all whitespace is unwanted. Spaces within sentences or data entries are usually required for proper readability. Thus, we typically only trim leading and trailing whitespace, not internal spaces.

Example of trimming fields using JavaScript

Here's a simple Javascript object containing data:

const data = [
  { Name: " John Doe ", Email: "john.doe@email.com", Address: "123 Main St" },
  { Name: "Jane Doe", Email: " jane.doe@email.com ", Address: "456 Elm St" },
  { Name: "Bob Smith", Email: "bob.smith@email.com", Address: " 789 Oak Ave " },
];

Here's a JavaScript function that trims the whitespace from all the strings in an object:

function trimFields(data) {
  return data.map((item) => {
    for (let field in item) {
      if (typeof item[field] === "string") {
        item[field] = item[field].trim();
      }
    }
    return item;
  });
}

This function uses the .trim() method, which removes whitespace from both ends of a string. This includes spaces, tabs, no-break spaces, and all the line terminator characters (LF, CR, etc). The function iterates over each field in each item of the data. If the field's value is a string, it trims that value.

Before

NameEmailAddress
John Doe john.doe@email.com123 Main St
Jane Doe jane.doe@email.com 456 Elm St
Bob Smithbob.smith@email.com 789 Oak Ave

After

NameEmailAddress
John Doejohn.doe@email.com123 Main St
Jane Doejane.doe@email.com456 Elm St
Bob Smithbob.smith@email.com789 Oak Ave

In the "Before" table, the extra spaces in the cells are intentional to illustrate the need for trimming. After trimming, these extra spaces are removed.

Alternative approach using regular expressions

If you want more control than the built-in .trim() method, you can also use the .replace() method with a regular expression (Regex) pattern.

item[field] = item[field].replace(/^[\s<>-]++|[\s<>-]+$/g, "");

This function uses a Regex pattern that matches leading (^\s+) and trailing (\s+$) whitespace as well as several other characters we wish to exclude (<, >, and -). The g flag is used to replace all matches (not just the first one). For each string field in each item of the data, it replaces leading and trailing whitespace with an empty string, effectively trimming the string.