isdigit(3f) - [M_strings:COMPARE] returns .true. if character is a digit (0,1,...,9) and .false. otherwise (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
elemental function isdigit(onechar)
character(len=*),intent(in) :: onechar logical :: isdigit
isdigit(3f) returns .true. if character is a digit (0,1,...,9) and .false. otherwise
onechar character to test
isdigit Returns true logical value if character is a "digit"
( an ASCII-7 character from the set {0,1,..,9}). That is, from CHAR(48) to CHAR(57) inclusive. If a multi-byte string was input .false. is returned if any character is not a digit. A null string returns .false.
Sample Program:
program demo_isdigitExpected output:use M_strings, only : isdigit, isspace, switch implicit none character(len=10),allocatable :: string(:) character(len=1),allocatable :: chars(:) character(len=*),parameter :: g=(*(g0,1x)) integer :: i
string=[& & 1 2 3 4 5 ,& & letters ,& & 1234567890 ,& & both 8787 ]
! if string is nothing but digits and whitespace return .true.
print g,using ISDIGIT(3) and ISSPACE(3): do i=1,size(string) ! convert to array of single characters chars=switch(string(i)) print g, For string[,string(i),], & & all( isdigit(chars) .or. isspace(chars) ) enddo
! ALTERNATIVE using VERIFY(3) ! the Fortran intrinsic function VERIFY(3) returns a position just ! not a logical like C, which can be useful for complex comparisons print g,using VERIFY(3): do i=1,size(string) print g, For string[,string(i),], & & verify(string(i), "01234567890 ") == 0 enddo
end program demo_isdigit
> using ISDIGIT(3) and ISSPACE(3): > For string[ 1 2 3 4 5 ] T > For string[ letters ] F > For string[ 1234567890 ] T > For string[ both 8787 ] F > using VERIFY(3): > For string[ 1 2 3 4 5 ] T > For string[ letters ] F > For string[ 1234567890 ] T > For string[ both 8787 ] F
John S. Urban
Public Domain
