荔园在线
荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀
[回到开始]
[上一篇][下一篇]
发信人: oopilix (优雅的), 信区: Visual
标 题: [zl]Character checking "@" in delphi
发信站: 荔园晨风BBS站 (Sun Jun 1 23:13:33 2003), 站内信件
1:program Project2;
{$APPTYPE CONSOLE}
var
I: Integer;
Tmp: string;
begin
repeat
ReadLn(Tmp);
I := Pos('@', Tmp);
if (I > 1) and (I < Length(Tmp)) then begin
inc(I);
while (I <= Length(Tmp)) and (Tmp[I] <> '@') do
Inc(I);
if (I <= Length(Tmp)) then begin
WriteLn('The @ occurs twice in the string.');
end
else begin
WriteLn('@ found only once, in the right location.');
end;
end
else begin
if (I = 0) then begin
WriteLn('@ not in string.');
end
else begin
WriteLn('@ at wrong location.');
end;
end;
until False;
end.
The previous code would ignore the fact that the string could end with '@@'...
And believe me, it does check for @ occuring twice in the input. First, I know
that the @ is at I. So next I go to the following character in the string and
search for a second occurance.
2:
procedure TForm1.Edit1Exit(Sender: TObject);
var
I: Integer;
Tmp: string;
begin
if (Pos('@', Edit1.Text)>0) = false
then
ShowMessage('none');
i := pos('@',edit1.text);
if i > 0 then
if pos('@',copy(edit1.text,(succ(i)),9999)) > 0 then
begin
i := 2;
if (i=2) then showmessage('2');
end
else
i := 1;
if (i=1) then showmessage('1');
end;
but the winner is Workshop_Alex. Your code popped up a consolebox, which wasn't
any good to me, so I tweaked a bit:
procedure TForm1.Edit1Exit(Sender: TObject);
var
I: Integer;
begin
I := Pos('@', Edit1.text);
if (I > 1) and (I < Length(Edit1.text)) then begin
inc(I);
while (I <= Length(Edit1.text)) and (Edit1.text[I] <> '@') do
Inc(I);
if (I <= Length(Edit1.text)) then begin
ShowMessage('The @ occurs twice in the string.');
end
else begin
ShowMessage('@ found only once, in the right location.');
end;
end
else begin
if (I = 0) then begin
ShowMessage('@ not in string.');
end
else begin
ShowMessage('@ at wrong location.');
end;
end;
end;
Works on a form now. Thanks both for your help :)
--
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 61.144.235.39]
[回到开始]
[上一篇][下一篇]
荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店