Reputation: 71
This is a new question of this topic:
How do I store and load a list of key-value pairs in a string?
I have come to the following code:
procedure TForm1.BotaoLimpaClick(Sender: TObject);
var
ListaSubstituicoes, Atual: String;
ListaLimpeza, Pares: TStringList;
i: Integer; //('O' , ' .' , '.' , '/' , '-');
begin
ListaSubstituicoes := '|O| = |0| , | .| = |.| , . , / , -';
TextoCompleto := Trim(EditTexto.Text);
ListaLimpeza := TStringList.Create;
Pares := TStringList.Create;
ExtractStrings([','],[], PChar(ListaSubstituicoes), ListaLimpeza);
for i := 0 to (ListaLimpeza.Count - 1) do
begin
Atual := ListaLimpeza[i];
Atual := Trim(Atual);
if Pos('=', Atual) = 0 then
begin
TextoCompleto :=
StringReplace(TextoCompleto, Atual, '', [rfReplaceAll, rfIgnoreCase]);
Continue;
end;
Pares.Clear;
ExtractStrings(['='],[], PChar(Atual), Pares);
Pares.Text :=
StringReplace(Pares.Text, '|', '', [rfReplaceAll, rfIgnoreCase]);
//Pares[1] := StringReplace(Pares[1], '|', '', [rfReplaceAll, rfIgnoreCase]);
TextoCompleto :=
StringReplace(TextoCompleto, Pares[0], Pares[1], [rfReplaceAll, rfIgnoreCase]);
end;
it's driving me crazy tho. When I apply it to the following:
75691 .30698 02053447138 05764.100011 5 572500000382o0
it simple does not work! It doesn't remove the ' .306' white space and it doesn't replaces the o with 0 in the end of the statement. Why is that? I believe it has to do with StringReplace not working correctly, probably it's not respecting the ' ' whitespaces, any clues?
Pares[0] is correctly getting the 'O' value and Pares[1] is correctly getting '0'. I've checked with trace into. But strangely, TextoCompleto := StringReplace(TextoCompleto, Pares[0], Pares[1], [rfReplaceAll, rfIgnoreCase]);
is not producing the desired result of replacing 572500000382o0
with 57250000038200
Upvotes: 1
Views: 1939
Reputation: 71
Crap, the only thing that was making the code not work was a lack of trim.
StringReplace( -> Trim <- (Pares.Text), '|', '', [rfReplaceAll, rfIgnoreCase]);
But I believe working with Pares.Text
like that is not good coding, so I've replaced it with:
ExtractStrings(['='],[], PChar(Atual), Pares);
Pares[0] := StringReplace(Trim(Pares[0]), '|', '', [rfReplaceAll, rfIgnoreCase]);
Pares[1] := StringReplace(Trim(Pares[1]), '|', '', [rfReplaceAll, rfIgnoreCase]);
Works like a charm.
Upvotes: 0
Reputation: 27377
I'm not sure about the desired result, as far as I could follow ...
const
ListaSubstituicoes = 'O=0, .=.';
var
ListaLimpeza: TStringList;
i: Integer;
TextoCompleto:String;
begin
TextoCompleto := Trim(EditTexto.Text);
ListaLimpeza := TStringList.Create;
try
ExtractStrings([','],[], PChar(ListaSubstituicoes), ListaLimpeza);
for i := 0 to (ListaLimpeza.Count - 1) do
begin
TextoCompleto := StringReplace(TextoCompleto, ListaLimpeza.Names[i], ListaLimpeza.ValueFromIndex[i], [rfReplaceAll, rfIgnoreCase]);
end;
Caption := TextoCompleto; // test
finally
ListaLimpeza.Free;
end;
end;
referring to your comment and link, you are pobably looking for something like this, of course "
could be replaced by e.g. |
const
ListaSubstituicoes = '"O"="0"," ."="."';
var
ListaLimpeza: TStringList;
i: Integer;
TextoCompleto:String;
begin
TextoCompleto := Trim(EditTexto.Text);
ListaLimpeza := TStringList.Create;
try
ExtractStrings([','],[], PChar(StringReplace(ListaSubstituicoes,'"','',[rfReplaceAll])), ListaLimpeza);
for i := 0 to (ListaLimpeza.Count - 1) do
begin
TextoCompleto := StringReplace(TextoCompleto, ListaLimpeza.Names[i], ListaLimpeza.ValueFromIndex[i], [rfReplaceAll, rfIgnoreCase]);
end;
Caption := TextoCompleto;
finally
ListaLimpeza.Free;
end;
end;
Upvotes: 2