len_white(3f) - [M_strings:LENGTH] get length of string trimmed of whitespace. (LICENSE:PD)
Synopsis
Description
Options
Returns
Example
Notes
Author
License
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_whiteuse M_strings, only : len_white character(len=80) :: s intrinsic len
s= ABCDEFG abcdefg ilen = len(s) lastnb = len_white(s)
write(*,*) total length of variable is ,ilen write(*,*) trimmed length of variable is ,lastnb write(*,*) trimmed string=[,s(:lastnb),]
end program demo_len_white
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 ilen=len(s) ! get total length of variable ! explicitly specify a substring instead of just variable name lastnb = len_white(s(:ilen)) write(*,*)error:[,s(:lastnb),] end subroutine messages
John S. Urban
Public Domain
len_white (3) | July 05, 2020 |