【问题标题】:Search numbers in different Memos and separate them into one在不同的备忘录中搜索号码并将它们分成一个
【发布时间】:2017-06-30 20:58:42
【问题描述】:

我无法在不同的备忘录中搜索字符串并将它们分开。 我们去现场吧。

在 Memo1 中我有以下文字

18049,25047,text4
18047,25046,text2
18048,25045,text3
18050,25048,text5
18046,25044,text1

在备忘录 2 中

25049,9012646205,55315135004,adou4
25047,"",06252782912,textasidh
25046,"",44425660030,textblabla
25048,"",07649186806,textaldj

我需要将第一个数字分隔到 memo2 的逗号处,然后提取到 memo1 中并添加完整的行。备忘录 1 + 备忘录 3 中的备忘录 2。

18046,25044,text1 25046,"",44425660030,textblabla
18047,25046,text2 25047,"",06252782912,textasidh
18048,25045,text3 25048,"",07649186806,textaldj
18049,25047,text4 25049,9012646205,55315135004,adou4

我已经尝试过使用function Split(Text, Delimitador: string): TSarray;,但没有成功

var
   I, J: Byte;
   Z : String;
begin
  for I := 1 to 2 do
  begin
    for J := 0 to TMemo(FindComponent('Memo'+IntToStr(I))).Lines.Count -1 do
      begin
        Z := Memo2.Lines[J];
        if Pos(Split(Z, ',')[0],TMemo(FindComponent('Memo'+IntToStr(I))).Lines[J]) > 0 then
        Memo3.Lines.Add(TMemo(FindComponent('Memo'+IntToStr(I))).Lines[J]);
      end;
  end;
end;

【问题讨论】:

  • 你看过这个问题吗? stackoverflow.com/questions/2625707/…
  • 另外,你应该总是发布一些代码来展示你失败的地方,以便其他人可以帮助你修复你的代码或你的想法。
  • 你为什么要使用备忘录?使用非 GUI 组件。
  • 嗨@DavidHeffernan,不需要使用gui界面,我只是用它来“促进”预览
  • 你从不给 Z 赋值,所以它是一个空字符串 (nil),在 Split 之后,你从结果空列表中取出第二个项目(索引 [1])。那肯定会出错。

标签: delphi


【解决方案1】:

解决了

var
I, J : byte;
begin
  for I := 0 to Memo1.Lines.Count -1 do
  begin
    for J := 0 to Memo2.Lines.Count -1 do
    begin
      if Pos(Split(Memo2.Lines[J], ',')[0],Memo1.Lines[I]) > 0 then
        Memo3.Lines.Add(Memo1.Lines[I]+' # '+Memo2.Lines[J]);
    end;
  end;
end;

【讨论】:

  • 宁可使用Integer 而不是Byte:如果Lines.Count0,那么Count - 1 是负数。通过使用Byte 作为局部变量,无论如何您都不会节省任何内存。对于这种情况,总是更喜欢使用Integer
  • FWIW,您不测试 Split 的结果是否为空。不要试图产生复杂的单行代码(如你的问题:J 循环是非常错误的)。让一切尽可能清晰,并在必要时使用变量。
  • 确实,Proper Code > Short Code
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
  • 2020-06-06
  • 2020-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多