Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
StringReplace未按预期工作_String_Delphi_Replace - Fatal编程技术网

StringReplace未按预期工作

StringReplace未按预期工作,string,delphi,replace,String,Delphi,Replace,这是这个话题的一个新问题: 我得到了以下代码: procedure TForm1.BotaoLimpaClick(Sender: TObject); var ListaSubstituicoes, Atual: String; ListaLimpeza, Pares: TStringList; i: Integer; //('O' , ' .' , '.' , '/' , '-'); begin ListaSubstituicoes := '|O| = |0| , | .|

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

我得到了以下代码:

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 5725000003820

它简单不起作用!它不会删除“.306”空格,也不会在语句末尾将o替换为0。为什么呢?我相信这与StringReplace不能正常工作有关,可能是因为它不尊重“空白”,有什么线索吗


Pares[0]正确获取“O”值,Pares[1]正确获取“0”。我和trace查过了。但奇怪的是,
TextoCompleto:=StringReplace(TextoCompleto,Pares[0],Pares[1],[rfReplaceAll,rfIgnoreCase])
没有产生用
57250000038200
替换
572500000382o0
所需的结果,就我所能做到的而言,我不确定所需的结果

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;

废话,唯一让代码不起作用的就是缺少修剪

StringReplace( -> Trim <- (Pares.Text), '|', '', [rfReplaceAll, rfIgnoreCase]);

工作起来很有魅力。

bummi,我已经用一个链接更新了OP,这样你就可以了解它的背景。我现在正在尝试,问题是,如果我有
listasubstituices='“O”=“0”、“=”;
ie.常量中的空白。要删除所有空白,如果需要,你可以使用listasubstituices='“O”=“0”、“=”"';是的,手动是的,但我希望自动=D,这样列表的视觉效果更容易理解(我将
,| O |=| 0 |,|.|=|.|,,,,/,-'
设置为
,“O”=“0”,“=”
)。但我有个好消息,我已经发现并解决了这个问题,我将把它发布在我自己问题的答案中,只是现在不能这样做,因为按照网站规则,必须等待8个小时。这真是一个修剪的问题!正如我提到的,替换
|
不是问题,我不明白
,,/,-”是什么部分可以。这里的代码太多了。将其归结为对
StringReplace
的单个调用。对两个字符串参数使用文本。您使用过调试器吗?其他人就是这样调查这类问题的。确认您正在将正确的值传递给被指控行为不端的函数。使用“监视”窗口和工具提示表达式计算器。我做到了。这正是我得出结论的原因,这是StringReplace的问题。您的更新不好。你为什么不能照我说的做?我其实并不关心这个问题。我关心的是教你如何解决问题。将一个问题简化为最小的复制是bug狩猎101。文字是指当你去掉变量和函数调用,并将一个值逐字写入你的代码中,而不是从其他值中计算出来。那么,您是说
StringReplace('75691.30698 02053447138 05764.100011 5 572500000382o0','O','0',[rfReplaceAl,rfIgnoreCase])
返回一个仍然包含字母O的字符串?运行准确的代码,并确认
StringReplace
的行为符合您的指控。如果没有,那就调查其他事情。这叫做调试。可惜你没有听从我的建议。这将使问题变得简单。对你来说也很简单。如果你不听劝告,为什么来这里寻求帮助?伙计,如果你想说什么,请说出来。你想让我向你致敬吗?你经常说的这个建议是什么?对我补充的问题的评论。如果你把这个问题归结为一行,那就很容易理解了。把什么归结为一行,问题是什么?没错。您可以用一行代码来说明这个问题。调用
StringReplace
。传递字符串文字,而不是传递变量。因此,字符串文字类似于这样的
'foo'
。将作为参数传递的变量替换为包含与变量相同值的字符串文字。正如@Rob在评论中所说,这是标准调试。
ExtractStrings(['='],[], PChar(Atual), Pares);
Pares[0] := StringReplace(Trim(Pares[0]), '|', '', [rfReplaceAll, rfIgnoreCase]);
Pares[1] := StringReplace(Trim(Pares[1]), '|', '', [rfReplaceAll, rfIgnoreCase]);