Skip to main content
Solved

Remove extra spaces within a string?

  • February 18, 2023
  • 2 replies
  • 197 views

Forum|alt.badge.img+4
  • New Participant
  • 4 replies

Hi! Is there a formula (or other solution) to remove extra whitespace within a string? My formula right now looks like: 

 

SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM({name})," "," ")," :",":"),” .”,”.”),” ?”,”?”),” ,”,”,”)

 

This works great to remove whitespace before/after the string using Trim and then remove double spaces and spaces before punctuation using Substitute. I'm wondering if there's a way to also remove instances of 3 or more spaces within a string without separately writing each possibility (3 spaces, 4 spaces, etc) as substitutes.

Thanks for any help!

Best answer by Jeff_Haskin

Here's a formula that will both remove spaces before punctuation AND correct multiple consecutive spaces all in one concise formula:

 

REGEX_REPLACE( REGEX_REPLACE( TRIM({name}), "\s+(\,|\;|\.|\-|\?|\:)", "$1" ), "\s+", " " )

 

This first trims the "name" field, then removes spaces before punctuation, then replaces any remaining instances of multiple consecutive spaces with a single space.

You can also change which punctuation it's looking for by simply adding or removing the desired punctuation from the list.

 

 

Enjoy!

2 replies

Forum|alt.badge.img+9
  • Inspiring
  • 24 replies
  • Answer
  • February 18, 2023

Here's a formula that will both remove spaces before punctuation AND correct multiple consecutive spaces all in one concise formula:

 

REGEX_REPLACE( REGEX_REPLACE( TRIM({name}), "\s+(\,|\;|\.|\-|\?|\:)", "$1" ), "\s+", " " )

 

This first trims the "name" field, then removes spaces before punctuation, then replaces any remaining instances of multiple consecutive spaces with a single space.

You can also change which punctuation it's looking for by simply adding or removing the desired punctuation from the list.

 

 

Enjoy!


Forum|alt.badge.img+4
  • Author
  • New Participant
  • 4 replies
  • February 18, 2023

Here's a formula that will both remove spaces before punctuation AND correct multiple consecutive spaces all in one concise formula:

 

REGEX_REPLACE( REGEX_REPLACE( TRIM({name}), "\s+(\,|\;|\.|\-|\?|\:)", "$1" ), "\s+", " " )

 

This first trims the "name" field, then removes spaces before punctuation, then replaces any remaining instances of multiple consecutive spaces with a single space.

You can also change which punctuation it's looking for by simply adding or removing the desired punctuation from the list.

 

 

Enjoy!


Thank you! That works perfectly! Very much appreciated!