Here's a common useful sub to remove spaces from both ends of a string. The string is passed to the sub and returned stripped.
REM
DEFLNG A-Z
SCREEN 12
b% = 123
a$ = STR$(b%)
'Trimer a$
PRINT a$, b%
SLEEP
SYSTEM
SUB Trimer (Temp$)
Temp$ = LTRIM$(RTRIM$(Temp$))
END SUB
You will note the leading space with the first printed value. Remove the REM to see the correctly trimmed result.