首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >StringReplace未按预期工作

StringReplace未按预期工作
EN

Stack Overflow用户
提问于 2013-06-04 20:47:24
回答 2查看 1.5K关注 0票数 1

这是这个话题的一个新问题:

How do I store and load a list of key-value pairs in a string?

我得到了以下代码:

代码语言:javascript
运行
复制
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的预期结果

EN

回答 2

Stack Overflow用户

发布于 2013-06-04 21:07:19

据我所知,我不确定期望的结果是什么。

代码语言:javascript
运行
复制
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;

参考你的评论和链接,你可能正在寻找类似这样的东西,当然可以用|代替"

代码语言:javascript
运行
复制
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;
票数 2
EN

Stack Overflow用户

发布于 2013-06-05 05:08:17

废话,唯一让代码不能工作的就是缺少修剪。

代码语言:javascript
运行
复制
StringReplace( -> Trim <- (Pares.Text), '|', '', [rfReplaceAll, rfIgnoreCase]);

但我认为这样使用Pares.Text不是很好的编码,所以我将其替换为:

代码语言:javascript
运行
复制
ExtractStrings(['='],[], PChar(Atual), Pares);
Pares[0] := StringReplace(Trim(Pares[0]), '|', '', [rfReplaceAll, rfIgnoreCase]);
Pares[1] := StringReplace(Trim(Pares[1]), '|', '', [rfReplaceAll, rfIgnoreCase]);

就像一种护身符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16918250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档