如何检查字符串中是否存在“@”

How to check if '@' is present in a string

我尝试使用 If 语句,但它只显示消息,如果我只将 @ 放在编辑中

var
sname , email : string ;
iAge , igrade : integer ;
iVal : string;

begin
ival := '@';
email :=  (edtEmail.Text);
if ival = email then


 begin


Showmessage(' Email Address must contain @ ');
 end;

的确,条件ival = emailtrue iff ivalemail是相同的字符串。由于 ival@,当且仅当 email 恰好是 @.

,条件为真

您想检查 @ 是否在 email 中。为此,您可以使用 Pos 函数,其中 returns 是字符串中第一次出现的子字符串的第一个字符的从 1 开始的索引,或者 0 是子字符串在字符串中找不到:

if Pos('@', email) = 0 then
  ShowMessage('The email address must contain @.');

请注意,实际上不需要变量来保存 at 字符。

现代版本的Delphi,最好写成

if not email.Contains('@') then
  ShowMessage('The email address must contain @.');

使用 TStringHelper.Contains,因为这样更容易阅读。

您可以使用 Pos 函数来检查 @ 字符是否可以在电子邮件字符串中找到。

if Pos('@', email) = 0 then
begin
   Showmessage(' Email Address must contain @ ');
end;

这是一篇关于在 Delphi

中验证电子邮件地址的文章

https://www.howtodothings.com/computers/a1169-validating-email-addresses-in-delphi.html