注册 登录
编程论坛 Delphi论坛

【求助】如何查找字符串中某个字符的数量

rosege 发布于 2008-12-08 15:31, 1821 次点击
请问:如何查找字符串中某个字符的数量?
比如,判断邮箱地址,用户是否重复输入多个“@”?等等。
例:字符串“abcabcabc”,如何判断该字符串里面有几个“a”?
1 回复
#2
rosege2008-12-08 20:54
已经解决,结贴。
函数如下:
function CheckEmail(const Email:string):Boolean;
begin
  Result:=True;
  if (Length(Email)-Length(StringReplace(Email,'@','',[rfReplaceAll])))<>1 then Result:=False;
  if Pos('@',Email)<=1 then Result:=False;
  if (Pos('.',Email)=Length(Email)) or (Pos('.',Email)<=Pos('@',Email)+1) then Result:=False;
end;

调用:
begin
  if Trim(EditEmail.Text)='' then Exit;
  if CheckEmail(EditEmail.Text)=False then
  begin
  Application.MessageBox('邮箱地址格式不正确','提示',64);
  EditEmail.SetFocus;
  end;
end;

[[it] 本帖最后由 rosege 于 2008-12-8 21:30 编辑 [/it]]
1