这是关于这个主题的新问题:
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'空格,也不会在语句末尾用0替换o。这是为什么?我认为它与StringReplace无法正常工作有关,可能它不尊重''空格,任何线索? Pares [0]正确获得'O'值并且Pares [1]正确地获得'0'。我已经检查了跟踪。但奇怪的是,TextoCompleto := StringReplace(TextoCompleto, Pares[0], Pares[1], [rfReplaceAll, rfIgnoreCase]);
没有产生用572500000382o0
替换57250000038200
所需的结果
答案 0 :(得分:2)
我不确定所希望的结果,据我所知......
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;
{{1}}
答案 1 :(得分:0)
Crap,唯一使代码无效的因素是缺乏修剪。
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]);
像魅力一样。