Wiki Home

All Trim


Namespace: VB
ALLTRIM removes leading and trailing spaces from a string. In VFP 9 and later, the trim commands can remove also any specified characters.

Note that when you store a string into a table, the string is padr'd to fill up the available space in the character field.

This means that this command has absolutely no effect:
REPLACE username WITH ALLTRIM(username)


Actually, that command could be used to take " mike " and change it to "mike ". Mike Yearwood.

To clarify - the crossed out command above has an effect. If a field contained space(3)+"Mike"+space(2), the first 3 spaces would be removed and the last 2, but the field would end up with "Mike" + the remaining number of spaces in the field.

This is the command that has absolutely no effect:
REPLACE username WITH TRIM(username)
(Note exception below regarding "varchar"s)

However, this could be very useful:

REPLACE ALL username WITH ALLTRIM(firstname) +" "+ ALLTRIM(lastname)

since field firstname probably has lots of white space after it.

Another alternative:
REPLACE ALL username WITH firstname - (" " + lastname)
VFP never fails to suprise me, though I think this use of the "-" operator for strings is pretty dubious. -- Tom(Paton)
Why "dubious"? It's been in the language since very early on. It's not going anywhere.--TamarGranor
It's dubious because one would never expect such a behaviour from the minusoperator, regardless of how long it is in FoxPro. IMHO this hurt's the principle of least surprise --Kurt
It's not dubious, it's a defined Operator. Trimming is reducing a string, thus what other operator would you come up with for reducing something, if not a MINUS? The fact, that other more stupid languages don't have that kind of operator, doesn't make it invalid. If all languages would be equal, life would be only half the fun. And as the saying goes: "You have to know your tool"... -- ?wOOdy

I agree with Tom. Since + operator when applied to a string means "add this string to this other string", the expected behavior for - operator applied to a string should be "strip this string from this other string if present", ex:

REPLACE test WITH "AB" + "C" --> "ABC"
REPLACE test WITH "AB" - "A" --> "B"

Sure, having an operator that automatically applies a TRIM() to a given string is very usefull when you need to concatenate some strings, but this behavior is not "obvious" (even if trimming a string is a kind of "substraction")... is dubios.

By the way, I didn't know this use of - operator on strings, and I'm using VFP since FoxBase+... so I agree with Tom about this too: after all this years, VFP never stops from surprising me. -- Victor Espina

The - operator does NOT trim the first string exactly, it reomoves trailing spaces from the first string and adds them to the concatenated string at the end. So;

lcStr1 = "Mike" + SPACE(3)
lcStr2 = "Smith" + SPACE(3)
lcStr3 = lcStr1 - lcStr2 && MikeSmith + SPACE(6)

--JimBooth

Notice: LEN(firstname + " " + lastname) = LEN(firstname-(" "+lastname))
When using VarChar fields reduce could be better

Another alternative:
REPLACE ALL username WITH Reduce(firstname + " " + lastname)
Reduce() is a function in Fox Tools.fll
--Kurt

Note that in VFP 9 and subsequent, VFP supports VarChar fields, and in this case trimming prior to REPLACE does affect what's stored.
Tom Cerul Steven Black
Category VFP Functions
( Topic last updated: 2006.11.13 07:14:42 AM )