while Pos(char(10), tempText) > 0 do
begin
tempText[Pos(char(10), tempText)] := '';
end;
Trouble is you can't use '' coz 'parantly this is not a character. Any
suggestions?
One clunky way that occurs, is:
1. loop
1.1. read characters up to char(10)
1.2. put all characters before char(10) into another temporary string
1.3. move tempText character pointer to next character after char(10)
1.4. goto 1.
1.5 end loop
while Pos(char(10), tempText) > 0 do
begin
Delete(tempText,Pos(#10,tempText),1);
end;
or StringReplace?
tempText := StringReplace(#10,tempText,'',[rfReplaceAll]);
--
This one was dead black, and in this, nothing moved.
>I'm trying to remove char(10) from a string.
>
> while Pos(char(10), tempText) > 0 do
> begin
> tempText[Pos(char(10), tempText)] := '';
> end;
>
>Trouble is you can't use '' coz 'parantly this is not a character. Any
>suggestions?
>
'' is a null (or empty) string. Characters always have a value, which can be 0
(or #0 which is a char of a zero value). But #0 is the terminator for a PChar
and most windows based stuff will not display beyond the #0.
If you want to replace the #10 then use some other char value, but if you want
to just concatenate the non-#10 characters, the best is most likely ...
function RemoveLineFeed : string;
var
i, j : integer;
const
LIneFeed : char = #10;
begin
SetLength(Result, Length(StrIn));
j := 1;
for i := 1 to Length StrIn do
if StrIn[i] <> LineFeed then begin
Result[j] := StrIn[i];
inc(j);
end;
{end for i := 1 to Length(StrIn)}
SetLength(Result, j - 1); // set final string to appropriate length
end;
Copying into a new string each time a #10 occurs (as you suggested) is
relatively slow because new memory has to be allocated for the string each
time. Doing it as above entails only two memory allocations.
If you really want to be fiendishly cunning you can copy the characters back
into the original string ...
var
i, j : integer;
const
LIneFeed : char = #10;
begin
j := 1;
for i := 1 to Length StrIn do
if StrIn[i] <> LineFeed then begin
StrIn[j] := StrIn[i]; // copy back into string, but j is always <= i
inc(j);
end;
{end for i := 1 to Length(StrIn)}
SetLength(StrIn, j - 1);
... and have only one memory allocation.
Alan Lloyd
alang...@aol.com