求一函数,判断一字符串是不是由数字和字母组成
比如:str='dfefe21erQEcccc';
str是由数字和字母组成(字母不分大小写都可以)。 

解决方案 »

  1.   

    我写了个最笨的方法,呵呵function TForm1.CheckStr(const S: string): Boolean;
    var
      i:Integer;
      a,b,c:Boolean;
    begin
      Result:=False;
      a:=False;
      b:=False;
      c:=False;
      for i:=0 to Length(s) do
      begin
        if S[i] in ['0'..'9'] then
        begin
          a:=True;
        end;
        if S[i] in ['a'..'z'] then
        begin
          b:=True;
        end;
        if S[i] in ['A'..'Z'] then
        begin
          c:=True;
        end;
      end;
      if (a and b) or (a and c) then Result:=True;
    end;
      

  2.   

    那你可以一个字符一个字符去判断
    STR[I]是不是在
    ‘a’-'z'
     'A'-'Z'
     '0'-'9'
    其他的就不是
    函数基本可以写成
    function CheckStr:Boolean
    var
     i:integer;
    begin
     result := True;
     for i := 1 to Length(str) do 
       begin
        if not (str[i] in['a'..'z']) then
             if not (str[i] in ['A'..'Z']then 
                if not (Str[i] in ['0'..'9'] then
                  begin
                    result := false;
                    break;
                  end   end;
    end;
      

  3.   


    str='dfefe21erQEcccc'; 
    j:= 0;
    K:= 0;
    for i:= 1 to length(str) - 1 do
    begin
    str1:= copy(str,i,1);
    try
     strtoint(str1);
     k:= K + 1;
    except
     j:= j+ 1; 
    end;
    end;
    //k为数字个数,j为字母个数
    if (k = 0) or (j = 0) then
    showmessage('不是字母和数字组成的字符串!');
    end;
      

  4.   


    function CheckStr:Boolean 
    var 
    i:integer; 
    begin 
    result := True; 
    for i := 1 to Length(str) do 
      begin 
        if not (str[i] in['a'..'z']) then 
            if not (str[i] in ['A'..'Z']then 
                if not (Str[i] in ['0'..'9'] then 
                  begin 
                    result := false; 
                    break; 
                  end   end; 
    end;
      

  5.   


    function IsAlphanumeric(const S: string): Boolean;
    const
      Alphanumerics = ['A'..'Z', 'a'..'z', '0'..'9'];
    var
      i : Integer;
    begin
      Result  := False;
      for i:=1 to Length(S)do
        if(not(S[i]in Alphanumerics))then Exit;
      Result  := True;
    end;
      

  6.   

    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}function isCheck(vStr:String):Boolean;
    var
      vCheck : byte;
      vChar : Pchar;
    begin
      Result := false;
      vChar := pchar(vStr);
      vCheck := 0;
      while vChar^ <> #0  do
      begin
        if vChar^ in ['0'..'9'] then
        begin
          if vCheck =2 then begin
            Result := True;
            break;
          end else  vCheck := 1;
        end;    if vChar^ in ['a'..'z','A'..'Z'] then
        begin
          if vCheck = 1 then begin
            result := true;
            break;
          end else vCheck := 2;
        end;
        inc(vChar);
      end;
    end;
    procedure TForm2.Button1Click(Sender: TObject);
    var
      Str : String;
    begin
      str :='dfefeerQEcccc';
      if isCheck(str) then showMessage('人老了') else showMessage('還未老');end;end.
    亂來的。呵呵
      

  7.   

    随便写了一个:procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
      str:string;
      sum:string;
    begin
      if edit1.Text='' then
        ShowMessage('请输入字符串')
      else
      begin
        sum:='';
        str:=Edit1.Text;
        for i:=1 to Length(str)do
        begin
          if str[i] in ['a'..'z','A'..'Z','0'..'9']then
            sum:=sum+str[i]
          else
          begin
            ShowMessage('No');
            Exit;
          end;
        end;
        ShowMessage(sum);
      end;  
    end;