有两个string数组,s1,s2
现在对s1中的数组进行和s2的比较
我用
if s1[i]=s2[j] then
怎么报错啊?好像类型的问题

解决方案 »

  1.   

    CompareText(s1[i],s2[j])=0 
    这样行吗?
      

  2.   

    CompareText(s1[i],s2[j])=0 
    这个肯定可以
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
     var
       S1 , S2 : String;
    begin
      // S1 := ...
      // S2 := ...
      if CompareText(S1[1],S2[2]) <> 0 then
        ShowMessage('两个是不一样的');
    end;
      

  4.   

    对于字符串,特别是参数传递有可能出现2中错误用法:1。类型定义的问题,应该在Type 中声明一种数组类型,不然2个数组的类型地址指的不一样,就报类型不同之类的错误
    type
       TArrString = Arry of string;
    end;
    S1, S1 : TArrString;
    然后对字符串进行操作
    2。对通常字符串比较常见错误就是 s1[i]=s2[j],应该自己写函数或用系统自带的函数CompareText,CompareStrings等等,具体使用请看Delphi帮助
      

  5.   

    用CompareText(S1[1],S2[2]) 安全,但是通常s1[i]=s2[j] 也不会有问题
    不然   char(ord(s1[i]))=char(ord(s1[j]))
      

  6.   

    var
      S1,S2 : String;
    begin
      S1 := 's1';
      S2 := 'S1';
      if strComp(PChar(S1),PChar(S2)) = 0 then // 区分大小写
        showmessage('Same')
      else
        Showmessage('Error');
      if striComp(PChar(S1),PChar(S2)) = 0 then  // 不区分大小写
        showmessage('Same')
      else
        Showmessage('Error');end;
      

  7.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function CmpStr(const StrS, StrD: string): Boolean;
      public
        { Public declarations }
      end;  TStudentRec = record
        No: string;
        Name: string;
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.CmpStr(const StrS, StrD: string): Boolean;
    var
      i: Integer;
    begin
      Result := False;
      if Length(StrS) <> Length(StrD) then Exit;
      for i := 0 to Length(StrS) - 1 do
        if StrS[i] <> StrD[i] then Exit;
      Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      Str1, Str2: string;
    begin
      Str1 := 'abc';
      Str2 := 'abc';
      if CmpStr(Str1, Str2) then ShowMessage('相同')
      else ShowMessage('不同');
    end;end.
      

  8.   

    没错啊,编译及运行都没问题
    var
      i : integer ;
      s1 , s2 : Array [0..9] of string ;
    begin
      for i := 0 to 9 do
        s1[i] := inttostr(i);
      for i := 0 to 9 do
        s2[9-i] := inttostr(i);
      if s1[8] = s2[1] then
        showmessage('s1=s2')
      else
        showmessage('s1<>s2');
    end;