新手学习delphi,TStringList简单问题求助,成功马上结贴。TStringList有如下数据
123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432取出每行字符串前两位字符进行对比 如果重复则删除 保留一行

123
124
只保留123  //遍历 错误
  for i:= 0 to sjList.Count - 1 do
  begin
  sjTemp := sjList.Strings[0];
  sjList.Delete(0);
  left12 := copy(sjTemp,1,2);
      for j:= 0 to sjList.Count - 1  do
      begin
         scTemp := sjList.Strings[j];
         if left12=Copy(scTemp,1,2) then
         begin
           sjList.Delete(j);
           scList.Add(sjTemp);
           ShowMessage(sjList.Text);
         end;
      end;
  end ;应该怎么写呢?只有20积分了。

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var s:TStringList;
        i:integer;
    begin
      s := TStringList.Create;
      s.Sorted := true;
      s.Text :=  Memo1.Lines.Text;
      for i := s.Count - 1 downto 1 do
      begin
        if leftstr(s[i],2) = leftstr(s[i-1],2) then
          s.Delete(i-1);
      end;
      Memo1.Lines.Text := S.Text;
    end;
      

  2.   

    从后向前遍历,第0项不要for i := sjList.Count - 1 downto 1 do
      

  3.   

    果然 for i := s.Count - 1 downto 1 do应该是  for i := s.Count - 1 downto 0 do