len_white(3f) - [M_strings:LENGTH] get length of string trimmed
  of whitespace.
  (LICENSE:PD)
integer function len_white(string)
 character(len=*) :: string
  len_white(3f) returns the position of the last character in
  string that is not a whitespace character. The Fortran90 intrinsic
  LEN_TRIM() should be used when trailing whitespace can be assumed
  to always be spaces.
  This procedure was heavily used in the past because ANSI FORTRAN
  77 character objects are fixed length and blank padded and the
  LEN_TRIM() intrinsic did not exist. It should now be used only when
  whitespace characters other than blanks are likely.
  string     input string whose trimmed length is being calculated
             ignoring all trailing whitespace characters.
  len_white  the number of characters in the trimmed string
Sample Program:
program demo_len_white
  use M_strings, only : len_white
  implicit none
  character(len=80) ::  s
  integer           :: lgth, lastnb
  intrinsic len
  s=' ABCDEFG abcdefg '
  lgth = len(s)
  lastnb = len_white(s)
  write(*,*) 'total length of variable is ',lgth
  write(*,*) 'trimmed length of variable is ',lastnb
  write(*,*) 'trimmed string=[',s(:lastnb),']'
 end program demo_len_white
Results:
 total length of variable is           80
 trimmed length of variable is           16
 trimmed string=[ ABCDEFG abcdefg]
o len_white
  is a resource-intensive routine. Once the end of
  the string is found, it is probably best to keep track of it in
  order to avoid repeated calls to len_white. Because they
  might be more efficient, consider looking for vendor-supplied or
  system-optimized equivalents. For example:
     o lnblnk - Solaris f77
     o len_trim - FORTRAN 90
o Some compilers seem to have trouble passing a string of variable length properly. To be safe, use something like this:
   subroutine message(s)
    character(len=*) :: s ! s is of variable length
       lgth=len(s)        ! get total length of variable
       ! explicitly specify a substring instead of just variable name
       lastnb = len_white(s(:lgth))
       write(*,*)'error:[',s(:lastnb),']'
   end subroutine messages
John S. Urban
Public Domain
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | string | 
elemental integer function len_white(string)
!  DEPRECATED. Use len_trim(3f),trim(3f) unless you might have trailing nulls (common when interacting with C procedures)"
!  John S. Urban, 1984, 1997-12-31
!  Note that if the string is blank, a length of 0 is returned; which is not a legal string length in Fortran77.
!  this routine used to return one instead of zero.
!   - mod 1:     1994
!                added null (char(0)) because HP and some Suns not padding
!                strings with blank, but with null characters; 1994 JSU
!   - mod 2:     1999
!                update syntax with INTENT(), ENDDO, no RETURN
!                still need instead of LEN_TRIM() because some systems stil pad CHARACTER with NULL
!-----------------------------------------------------------------------------------------------------------------------------------
! ident_15="@(#) M_strings len_white(3f) return position of last non-blank/non-null character in string"
character(len=*),intent(in):: string ! input string to determine length of
integer                    :: i10
intrinsic len
   len_white=0
   do i10=len(string),1,-1
      select case(string(i10:i10))
      case(' ')                 ! space(32)
      case(char(0))             ! null(0)
      case(char(9):char(13))    ! tab(9), new line(10), vertical tab(11), formfeed(12), carriage return(13)
      case default
         len_white=i10
         exit
      end select
   enddo
end function len_white