Specific functions: Trim

The trim function is a deceptively simple one. All it does is remove excess spaces from a string. However, it makes my list of favorite text manipulation functions because it’s applicable in so many different situations. Like my other favorite text manipulation functions, I use it for proofreading manuscripts. It’s quite useful when manipulating text files, such as MARC records and video game FAQs. It’s also good when working with information from the web, like job postings and lists of available journals. I frequently use it to clean up my concatenates, and I understand that it can also be helpful when cleaning up results from SPSS. I find it invaluable when working with someone else’s code (either HTML or programming), because the indentation is often different than what I find easiest to read.

In general, “removing excess spaces” means removing all leading spaces (the spaces at the beginning), all trailing spaces (the spaces at the end), and all reducing all double (or triple, or quadruple, etc.) spaces to single spaces. It’s worth noting that this isn’t a grammatical cleanup — if you have a space before a comma, it’ll leave it there.

In the case of TRIM, all the programs pretty much have the same syntax and results. As always, “text1” means any string of text.

In Excel 97, in Excel 2007, in Google Spreadsheet, in OpenOffice Calc, and in Microsoft Works Spreadsheet:

  • Syntax: TRIM(text1)
  • Category: Text

Note: If you look at a cell in Google Spreadsheet, it looks like it’s already had trim applied. However, if you look up at the formula bar you’ll see all the excess spaces. That’s because it’s browser-based, and browsers don’t show excess white space. But the spaces are still there, and could mess you up if you’re working with other formulas.