Mega Code Archive

 
Categories / Delphi / Strings
 

How to check if a String is numeric

Title: How to check if a String is numeric {1.} function IsStrANumber(const S: string): Boolean; var P: PChar; begin P := PChar(S); Result := False; while P^ #0 do begin if not (P^ in ['0'..'9']) then Exit; Inc(P); end; Result := True; end; {2.} function IsStrANumber(const S: string): Boolean; begin Result := True; try StrToInt(S); except Result := False; end; end; Usage Example: procedure TForm1.Button1Click(Sender: TObject); begin if IsStrANumber(edit1.Text) = True then ShowMessage('String contains only numbers!'); end;