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;
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.
没错啊,编译及运行都没问题 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;
这样行吗?
这个肯定可以
var
S1 , S2 : String;
begin
// S1 := ...
// S2 := ...
if CompareText(S1[1],S2[2]) <> 0 then
ShowMessage('两个是不一样的');
end;
type
TArrString = Arry of string;
end;
S1, S1 : TArrString;
然后对字符串进行操作
2。对通常字符串比较常见错误就是 s1[i]=s2[j],应该自己写函数或用系统自带的函数CompareText,CompareStrings等等,具体使用请看Delphi帮助
不然 char(ord(s1[i]))=char(ord(s1[j]))
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;
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.
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;