delphi中的字符比较问题

jiangjian0427 2003-12-10 12:47:37
有两个string数组,s1,s2
现在对s1中的数组进行和s2的比较
我用
if s1[i]=s2[j] then
怎么报错啊?好像类型的问题
...全文
102 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
w1w2w3w4 2003-12-10
  • 打赏
  • 举报
回复
是不是超过界限啦。
yang_113 2003-12-10
  • 打赏
  • 举报
回复
没错啊,编译及运行都没问题
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;
qiume 2003-12-10
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
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.
tglong 2003-12-10
  • 打赏
  • 举报
回复
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;
「已注销」 2003-12-10
  • 打赏
  • 举报
回复
学习
delphi_xizhousheng 2003-12-10
  • 打赏
  • 举报
回复
用CompareText(S1[1],S2[2]) 安全,但是通常s1[i]=s2[j] 也不会有问题
不然 char(ord(s1[i]))=char(ord(s1[j]))
「已注销」 2003-12-10
  • 打赏
  • 举报
回复
对于字符串,特别是参数传递有可能出现2中错误用法:

1。类型定义的问题,应该在Type 中声明一种数组类型,不然2个数组的类型地址指的不一样,就报类型不同之类的错误
type
TArrString = Arry of string;
end;


S1, S1 : TArrString;
然后对字符串进行操作


2。对通常字符串比较常见错误就是 s1[i]=s2[j],应该自己写函数或用系统自带的函数CompareText,CompareStrings等等,具体使用请看Delphi帮助
ExploiterSoft 2003-12-10
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
S1 , S2 : String;
begin
// S1 := ...
// S2 := ...
if CompareText(S1[1],S2[2]) <> 0 then
ShowMessage('两个是不一样的');
end;
ExploiterSoft 2003-12-10
  • 打赏
  • 举报
回复
CompareText(s1[i],s2[j])=0
这个肯定可以
Alanwn 2003-12-10
  • 打赏
  • 举报
回复
CompareText(s1[i],s2[j])=0
这样行吗?

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧