Author: Jonas Bilinkevicius
We need a workable function that can strip embedded characters (single qoutes,
double quotes, etc.,) from within string vars.
Answer:
Solve 1:
Here is a general method to remove characters from a string:
1 type2 TCharSet = setof Ansichar;
3 4 procedure RemoveCharacters(var S: AnsiString; const characters: TCharset);
5 var6 i: Integer;
7 begin8 for i := Length(S) downto 1 do9 if S[i] in characters then10 delete(S, i, 1);
11 end;
12 13 14 //In your case you would call it as:15 16 17 RemoveCharacters(aString, [' ']);
18 19 20 //There are certainly faster ways to implement this but unless you call the 21 procedure some ten-thousand times in a loop I would not worry about that.
Solve 2:
22 23 function RemoveCharsFromString(const TheString: string; const CharsToRemove: array24 of Char): string;
25 var26 i:
27 Integer;
28 begin29 Result := TheString;
30 for i := Low(CharsToRemove) to High(CharsToRemove) do31 begin32 Result := StringReplace(Result, CharsToRemove[i], '', [rfReplaceAll]);
33 end;
34 end;
Solve 3:
35 type36 TSetOfChar = setof char;
37 38 function RemoveCharsFromString(const TheString: string;
39 const CharsToRemove: TSetOfChar): string;
40 var41 i, j: Integer;
42 begin43 SetLength(Result, length(TheString));
44 j := 0;
45 for i := 1 to length(TheString) do46 begin47 ifnot (TheString[i] in CharsToRemove) then48 begin49 inc(j);
50 Result[j] := TheString[i];
51 end;
52 end;
53 SetLength(Result, j);
54 end;