Beranda > Delphi > How to check if a string is a number

How to check if a string is a number

Checking if a string is a number is quite simple, all we have to do is check if all it’s characters are numbers. The situation becomes more complicated when you wish to recognize symbols (‘+’, ‘-‘, ‘$’ etc.), but I’ll leave that out for now. This code uses PChar are the parameter due to speed. If you want to send a string value to the function just typecast it.

function IsNumber(pcString: PChar): Boolean;
begin
  Result := False;
  while pcString^ <> #0 do // 0 indicates the end of a PChar string
    if not (pcString^ in ['0'..'9']) then
      Exit;
    Inc(pcString);
  end;
end;
See an extended version of this routine – IsNumeric – in the Code Snippets Database. This routine can optionally check for floating point numbers.
Iklan
Kategori:Delphi
  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: