Manual Reference Pages  - len (3fortran)

NAME

LEN(3) - [CHARACTER] Length of a character entity

SYNOPSIS

result = len(string [,kind])

         integer(kind=KIND) function len(string,KIND)

character(len=*),intent(in) :: string(..) integer,optional,intent(in) :: KIND

CHARACTERISTICS

o STRING is a scalar or array character variable
o KIND is a scalar integer constant expression.
o the returned value is the same integer kind as the KIND argument, or of the default integer kind if KIND is not specified.

DESCRIPTION

LEN(3) returns the length of a character string.

If STRING is an array, the length of a single element of STRING is returned, as all elements of an array are the same length.

Note that STRING need not be defined when this intrinsic is invoked, as only the length (not the content) of STRING is needed.

OPTIONS

o STRING : A scalar or array string to return the length of. If it is an unallocated allocatable variable or a pointer that is not associated, its length type parameter shall not be deferred.
o KIND : A constant indicating the kind parameter of the result.

RESULT

The result has a value equal to the number of characters in STRING if it is scalar or in an element of STRING if it is an array.

EXAMPLES

Sample program

    program demo_len
    implicit none

! fixed length character(len=40) :: string ! allocatable length character(len=:),allocatable :: astring character(len=:),allocatable :: many_strings(:) integer :: ii ! BASIC USAGE ii=len(string) write(*,*)’length =’,ii

! ALLOCATABLE VARIABLE LENGTH CAN CHANGE ! the allocatable string length will be the length of RHS expression astring=’ How long is this allocatable string? ’ write(*,*)astring, ’ LEN=’, len(astring) ! print underline write(*,*) repeat(’=’,len(astring)) ! assign new value to astring and length changes astring=’New allocatable string’ write(*,*)astring, ’ LEN=’, len(astring) ! print underline write(*,*) repeat(’=’,len(astring))

! THE STRING LENGTH WILL BE CONSTANT FOR A FIXED-LENGTH VARIABLE string=’ How long is this fixed string? ’ write(*,*)string,’ LEN=’,len(string) string=’New fixed string ’ write(*,*)string,’ LEN=’,len(string)

! ALL STRINGS IN AN ARRAY ARE THE SAME LENGTH ! a scalar is returned for an array, as all values in a Fortran ! character array must be of the same length. many_strings = [ character(len=7) :: ’Tom’, ’Dick’, ’Harry’ ] write(*,*)’length of ALL elements of array=’,len(many_strings)

! NAME%LEN IS ESSENTIALLY THE SAME AS LEN(NAME) ! you can also query the length (and other attributes) of a string ! using a "type parameter inquiry" (available since fortran 2018) write(*,*)’length from type parameter inquiry=’,string%len ! %len is equivalent to a call to LEN() except the kind of the integer ! value returned is always of default kind.

! LOOK AT HOW A PASSED STRING CAN BE USED ... call passed(’ how long? ’)

contains

subroutine passed(str) character(len=*),intent(in) :: str ! the length of str can be used in the definitions of variables ! you can query the length of the passed variable write(*,*)’length of passed value is ’, LEN(str) end subroutine passed

end program demo_len

Results:

     >  length =          40
     >   How long is this allocatable string?  LEN=          38
     >  ======================================
     >  New allocatable string LEN=          22
     >  ======================
     >   How long is this fixed string?          LEN=          40
     >  New fixed string                         LEN=          40
     >  length of ALL elements of array=           7
     >  length from type parameter inquiry=          40
     >  length of passed value is           11

STANDARD

FORTRAN 77 ; with KIND argument - Fortran 2003

SEE ALSO

len_trim(3), adjustr(3), trim(3), and adjustl(3) are related routines that allow you to deal with leading and trailing blanks.

Functions that perform operations on character strings, return lengths of arguments, and search for certain arguments:
o ELEMENTAL: ADJUSTL(3), ADJUSTR(3), INDEX(3), SCAN(3), VERIFY(3)
o NONELEMENTAL: LEN_TRIM(3), LEN(3), REPEAT(3), TRIM(3)
fortran-lang intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 len (3fortran) April 28, 2024
Generated by manServer 1.08 from 4adceb89-3c6a-42b5-8470-fb44e049fe92 using man macros.