2014-10-16 78 views
-4

只处理帐号查询,投诉和建议以及有效的帐号,编写一个名为ValidateAccNum的子程序,接收帐号作为字符串,指示天气是否有效。有效帐号必须符合以下条件:如何验证字符串中的第一个字符是字母还是数字?

  • 帐号必须只有SEVEN个字符。

  • 帐号必须以字母开头。

我遇到的问题是计算出帐号的第一个字符是字符串还是整数。我的继承人代码:

procedure TfrmQuestion3.ValidateAccNum(AccNum: string); 
var 
    RealACCNum : Boolean; 
    ACCNumLength : Integer; 
    StartACCNum : string; 
begin 
    RealACCNum := False; 
    ACCNumLength := Length(AccNum); 
    StartACCNum := AccNum[1]; 

    If (ACCNumLength = 7) and (StartACCNum = string) // <--- This is obviously the 
                //  problem, I know its wrong 
    then RealACCNum = True 
    else exit; 
    end; 

那么,如何检查天气的一个字符的字符串或整数?

+1

'if(Length(AccNum)= 7)and IsCharAlpha(AccNum [1])then ...'(for Delphi 7) – TLama 2014-10-16 11:12:29

+1

@TLama Legend。谢谢你:) – 2014-10-16 11:14:42

+0

小旁注,你忘了检查其他6个字符是否代表一个数字...... – whosrdaddy 2014-10-20 06:20:31

回答

1
if (Length(AccNum) = 7) and IsCharAlpha(AccNum[1]) then 

您可以检查这样的^
                                                                            |

+0

那么这些变量没有任何用处? ('ACCNumLength'和'StartACCNum') – 2014-10-16 11:20:29

+1

顺便说一句,在尝试使用[]访问任何字符之前,养成检查字符串是否为空的习惯是个好主意。如果MyString为空,MyString [1]应该返回什么?在答案中,对Length()的检查当然会这样做,但你的q中没有。 – MartynA 2014-10-16 11:22:41

相关问题