这是这个话题的一个新问题:
How do I store and load a list of key-value pairs in a string?
我得到了以下代码:
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;尽管如此,我还是快疯了。当我将其应用于以下内容时:
75691 .30698 02053447138 05764.100011 5 572500000382o0
它很简单,不起作用!它不会删除‘.306’空格,也不会将语句末尾的o替换为0。为什么会这样呢?我相信这与StringReplace不能正常工作有关,可能是它没有尊重“”空格,有什么线索吗?
Pares正确获取“O”值,而Pares1正确获取“0”。我已经查过了。但奇怪的是,TextoCompleto := StringReplace(TextoCompleto, Pares[0], Pares[1], [rfReplaceAll, rfIgnoreCase]);并没有产生用57250000038200替换572500000382o0的预期结果
发布于 2013-06-04 21:07:19
据我所知,我不确定期望的结果是什么。
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;参考你的评论和链接,你可能正在寻找类似这样的东西,当然可以用|代替"
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;发布于 2013-06-05 05:08:17
废话,唯一让代码不能工作的就是缺少修剪。
StringReplace( -> Trim <- (Pares.Text), '|', '', [rfReplaceAll, rfIgnoreCase]);但我认为这样使用Pares.Text不是很好的编码,所以我将其替换为:
ExtractStrings(['='],[], PChar(Atual), Pares);
Pares[0] := StringReplace(Trim(Pares[0]), '|', '', [rfReplaceAll, rfIgnoreCase]);
Pares[1] := StringReplace(Trim(Pares[1]), '|', '', [rfReplaceAll, rfIgnoreCase]);就像一种护身符。
https://stackoverflow.com/questions/16918250
复制相似问题