How to remove trailing spaces in calc

I can’t find an answer anywhere that works.

I have several columns with lots of trailing spaces…some of them have several spaces at the end…up to 10-15 spaces.

I’ve tried this ^\s+|\ s+$ and it found a couple of cells with 1 leading space but doesn’t find the trailing spaces.
Can anyone help out on this? It’s driving me mad.
Thanks

^\s+|\ s+$ has some strange space between \ and s :wink:

Hello

to remove trailling spaces, I use [ ]*$ (Just to be sure: there is a space character within the brackets and option Regular expressions ticked in Find & Replace)

I’d rather use +$ (note the expression starts with a space character which isn’t that visible…) so a replacement is only done when there actually is at least one trailing space. Using * matches also if there is none and the content is unnecessarily replaced with itself. Also, the character class with a single character just complicates the expression internally, a single blank character is sufficient in this case.

+$ doesnt find anything in a test (LO 6.3.0.4). May be you meant \s+$ or [ ]+$

It works, as said the expression starts with a space so it is " +$" not “+$”. The Highlighting munges that.

If, in addition to removing trailing spaces, you don’t mind also removing leading spaces and extra spaces between words, the TRIM function might do what you want. For example, if your column for entries with unwanted spaces is A, and your column for entries with the unwanted spaces removed is B, you’d type =TRIM(A1) in cell B1, and then copy and paste that formula down the rest of the B column as far as necessary.

If you need to preserve leading spaces and extra spaces between words, I think you would in fact have to use regular expressions, whether in a formula or in a search-and-replace. Would something like \h+$ (match any horizontal white space at the end of a line) do the trick? (Sorry I can’t be more helpful here. I use Writer a lot more heavily than Calc, and in Writer I use the AltSearch extension. I end up seeing regular expressions in my search and replace fields but not really having to learn them, since AltSearch supplies them for me.)

Text contents being suspected to have trailing spaces may also have unwanted leading spaces, and probably sequences of spaces inside where only a single space is expected. The TRIM() function removes all 3 kinds of superfluous spaces as @PeterCM already explained.

To get the same result, at least concerning ordinary spaces (ASCII U+0020), as with the TRIM() function,
F&R with
Find: ^ *|(?<= ) +| *$ and Replace with: empty string
should work. This way it can be done “in situ”.
You may use ^\s*|(?<=\s)\s+|\s*$ to address the wider range of whitespace.

BTW: In LibO V6.3.3.2 TRIM() doesn’t work as specified (in 6.20.24 OpenFormula) with respect to the generalized usage of “space”.