len_white(3f) - [M_strings:LENGTH] get length of string trimmed of whitespace. (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Notes
Author
License
elemental 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(3) 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(3) 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_whiteResults: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
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
Nemo Release 3.1 | len_white (3m_strings) | January 10, 2025 |