Brian Dunning's FileMaker Custom Functions

Supertrim ( text )

Speedily (and with no recursion) remove leading and trailing white space (including spacing, non-breaking spaces, tabs, returns and line feeds) from a text string.

  Average rating: 4.2 (51 votes) Log in to vote

Debi Fuchs   Debi Fuchs - Show more from this author
Aptworks Consulting
http://www.aptworks.com

Share on Facebook Share on Twitter

  Sample input:
Supertrim(" John ¶ Smith¶")
  Sample output:
"John ¶ Smith"

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

CUSTOM FUNCTION: Supertrim ( text )
Written by Debi Fuchs of Aptworks Consulting

Speedily (and with no recursion) remove leading and trailing white space (including spacing, non-breaking spaces, tabs, returns, and line feeds) from a text string.

EXAMPLE:
Supertrim(" John ¶ Smith¶") // --> "John ¶ Smith"

LAST MODIFIED: 29-DEC-2024 by Debi Fuchs of Aptworks Consulting

 

Comments

Gerold   Gerold, Süstedt / Germany
Sep 4, 2014
Hmm -- what happens if no leading whitespace exists?

As far as I get it there ain't no output at all in this case?!

Suggestion for the result:

Case
(
first_char ; Middle ( text ; first_char ; last_char - first_char + 1 ) ;
last_Char < Length ( text ) ; Left ( text ; last_char ) ;
text
)
 
Erik S.   Erik S., NYC
Sep 19, 2014
Added removing Line Feeds:

Let(
[
// Determine value of original string with ALL whitespace removed.
t2 = Substitute( text; [ " "; "" ]; [ " "; "" ]; [ " "; "" ]; [ "¶"; "" ]; [ char ( 10 ); "" ] );

// Determine position of first non-ws character in original string.
first_char = Position( text; Left( t2; 1 ); 0; 1 );

// Determine position of last non-ww character in original string.
last_char = Position( text; Right( t2; 1 ); Length( text ); -1 )
];

// If any non-whitespace characters exist return appropriate
// middle portion of original text.

Case( first_char; Middle( text; first_char; last_char - first_char + 1 ) )
)
 
Debi Fuchs   Debi Fuchs, Aptworks Consulting
Sep 22, 2014
Gerold,

You said: "Hmm -- what happens if no leading whitespace exists? As far as I get it there ain't no output at all in this case?!"

Thanks for the feedback. This is not my experience. Would you mind providing an example?

For me, the example:
supertrim("test ¶ ")
yields:
test

Best,
Debi
 
Debi Fuchs   Debi Fuchs, Aptworks Consulting
Sep 22, 2014
Thanks, Erik S.! I have modified the function, giving you credit for the mod!
 
Carl Starr   Carl Starr, Los Ranchos
Jan 28, 2015
Thanks for this function - It works great!

I use it as an auto-enter calculation in user data entry fields:

Supertrim ( Self )
 
Stephen P   Stephen P, Perth, Western Australia
Jul 20, 2015
Thanks for this fantastic function Debi.

One of my users copied and pasted text from and html email into a field which supertrims by auto-enter calculation.

It still had a lot of trailing spaces and would not trim. After investigating I found that non breaking spaces had been copied.

I added [ Char(160); "" ] to the t2.

Just in case that helps anyone else.
 
H   H, CHCH
Nov 17, 2015
Fantastic!
 
Eric M.   Eric M., San Jose, CA
Jan 24, 2016
It seems the result documented in the example comment is incorrect:

EXAMPLE:
Supertrim(" John Smith¶") // --> "John Smith¶"

Should be:

EXAMPLE:
Supertrim(" John Smith¶") // --> "John Smith"

Since it removes the last "¶".
 
GaryODS   GaryODS, Sacramento
Sep 5, 2016
I've never written a custom function, so please forgive any oversights in this comment.

If I drop the space removal from the Substitute function, and add the char(160) how is Supertrim faster or different from

Trimall(Substitute( text; [ " "; "" ]; [char(160); "" ]; [ " "; "" ]; [ "¶"; "" ]; [ char(10); "" ] )) ?
 
Debi   Debi, Boston
Sep 6, 2016
Gary, the Supertrim removes leading and trailing whitespace characters, touching nothing within the middle of the text, e.g. "ban" & Supertrim(" ¶anas¶sour cherries¶pe ¶") & "ears" = "bananas¶sour cherries¶pears".
 
Paolo   Paolo
Sep 30, 2016
good deal, thanks !
 
Florence Haseltine   Florence Haseltine, Alexandria,VA
Jan 23, 2017
Great Custom Function. Especially when copy and pasting information from Web Sites.
 
Michael Rocharde   Michael Rocharde, Golden, CO
Nov 10, 2017
Absolutely invaluable.
 
nonio   nonio, lopuu
Aug 9, 2020
Thanks for this function.
Question: why is SPACE being substituted twice in t2?
 
Victor Hugo Alfaro Sequeira   Victor Hugo Alfaro Sequeira, Independiente
Apr 11, 2021
Buena Tarde. Tengo un campo Texto que es generado por un guion y me da esta información:
CL-180510
CL-199296
C-162179

RESULTADO DEBE SER: CL-180510, CL-199296, C-162179.

USE EL SuperTrim(Text) Y NO ME LO HACE.

Me podría ayudar por favor. Gracias. Espero.
 
Debi Fuchs   Debi Fuchs, Aptworks Consulting
Apr 14, 2021
Try this: Substitute( Text; "¶"; ", " )
 
Daniel   Daniel
Mar 28, 2023
Regarding nonio's question: "why is SPACE being substituted twice in t2?"

I see this as well. Perhaps all the quoted whitespace items should be replaced with a Char() function? I suspect there is something happening with web/browser/server character encoding.

Maybe those are two different types of spaces, but they are *not* being presented as such on this website for me in Safari. Both of those quoted spaces evaluate to "32" when run through Code().
 
Debi Fuchs   Debi Fuchs, Aptworks Consulting
Dec 29, 2024
Thanks, Nonio and Daniel. The second space was originally a non-breaking space. I have substituted char(160) in it's place so it will appear correctly on the web site.
 
Debi Fuchs   Debi Fuchs, Aptworks Consulting
Dec 29, 2024
Apologies for a the grammer. I meant "its place" not "it's place"!
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 21 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: