原帖:var   str:tStrings; 
        i:integer; 
begin 
    str:=tstrings.Create; 
    str.LoadFromFile('test.txt'); 
    i:=str.IndexOf('■');   //或     i:=Pos(str.Text,'■'); 
    showmessage(inttostr(i)); 
    str.Free; 
end; 
======================= 
      编辑时出现3个warning   ,分别说Tstrings   包含了抽象方法:tstrings.clear;tstrings.delete和tstrings.insert,没有其他error。但是运行.exe文件时却出现错误:abstract   error. 
已用F8调试,问题出在   i:=str.IndexOf('■')   这句。 
        函数的目的就是得到txt   文本中字符'■'的位置,然后再分段取出字符串。
我的研究结果是,■这是一个双字节字符,而TStrings,或者TStringList的IndexOf内部用的是AnsiCompareStr(长字符串比较)引起的错误,
解决办法是,从TStringList继承一个新类,覆盖这个函数function CompareStrings(const S1, S2: string): Integer;或者覆盖IndexOf
当然原帖没有从TstringList创建新类也是问题。呵呵

解决方案 »

  1.   

    我的代码
    var
      pc: PChar;
      i: Integer;
      s:string;
    begin
      Memo1.Lines.LoadFromFile('1.txt');
      s:=Memo1.Lines.Text;
      pc:=Memo1.Lines.GetText;
      OutputDebugString(pc);
      for i := 0 to Length(s) do
      begin
        OutputDebugString(PChar(IntToStr(i)+'----'+ IntToStr(Ord(pc^))+ '='+ pc^));
        pc:=pc+1;
      end;
    end;
    用"View|Debug Window|Event log"查看结果
      

  2.   

    TStrings是个虚类,其Get()、GetCount()、Clear()、Delete()、Insert()都是virtual; abstract;声明
    即只声明了接口,没有实际的执行代码
    楼主的问题需要使用其派生类TStringList来处理
    参考如下代码:
    var
      Str: TStrings;
      I: Integer;
    begin
      Str := TStringList.Create;
      try
        Str.LoadFromFile('test.txt');
        I := Str.IndexOf('■'); //或 I := Pos(Str.Text, '■');
        ShowMessage(IntToStr(I));
      finally
        Str.Free;
      end;
    end;另外:
    I := Str.IndexOf('■'); //或 I := Pos(Str.Text, '■');
    中TStrings.IndexOf()方法的作用是得到字符串所在的行数
    而Pos()函数则是得到子串所在字符串的位置
    两个作用是不同的
      

  3.   

    如果分析VCL代码,你会发现TMemo中的Lines也是使用TStrings的派生类TMemoStrings
    因为TStrings是不能直接使用的
    constructor TCustomMemo.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      Width := 185;
      Height := 89;
      AutoSize := False;
      FWordWrap := True;
      FWantReturns := True;
      FLines := TMemoStrings.Create; //<<<<<<<<<
      TMemoStrings(FLines).Memo := Self;
    end;楼主的代码风格要加强
    大小写和声明的地方要一致
    参考标准代码加入适当的空格
    整洁的代码会让人看着舒服
    反之,凌乱的代码让人难受
      

  4.   

    TStrings是基类,不能直接使用
      

  5.   

    呵呵,伴水老大来了,欢迎,我没有表达清楚,昨天我看到有个帖子叫Tstrings 取txt文件时的问题,原文,我在我的帖子引用了一下,就是,“我的研究结果”前面的内容,后面是我研究的结果,不是我要提问,呵呵,^_^