PDA

View Full Version : Trim bug?


Richard
11-01-2006, 09:32 AM
Trim, Ltrim and Rtrim all appear to work only if there is a non blank character in the string.

// This works:
a$ = " x ";
a$ = trim(a$);
showmessage("1: a$='" % a$ % "' length=" % length(a$));
// Shows "1: a$='x' length=1"

// But this is not what I wanted
a$ = " ";
a$ = trim(a$);
showmessage("2: a$='" % a$ % "' length=" % length(a$));
// Shows "2: a$=' ' length=3"

// I would have expected/hoped that the second message
// would have been: "2: a$='' length=0"

// To get round this, I wrote a replacement function xTrim thus:

a$ = " x ";
a$ = xTrim(a$);
showmessage("3: a$='" % a$ % "' length=" % length(a$));

a$ = " ";
a$ = xTrim(a$);
showmessage("4: a$='" % a$ % "' length=" % length(a$));

// and here's my function code

func xTrim(txt$)
Local (a$, b$);
a$ = txt$;
while (length(a$) > 0)
b$ = length(a$) - 1;
if (a$[b$] == 32)
a$ = mid(a$, 0, b$); // Right trim
else if (a$[0] == 32)
a$ = mid(a$, 1, -1); // Left trim
else
break;
end;
end;
return (a$);
end;

// Sorry about the lack of indentation, trim works in the forum

kornalius
11-01-2006, 04:31 PM
This has just been fixed for 1.07.

Thanks