Manual Reference Pages  - len (3m_unicode)

NAME

LEN(3f) - [M_unicode:WHITESPACE] Length of a string (LICENSE:MIT)

CONTENTS

Synopsis
Characteristics
Description
Options
Result
Examples
See Also
Author
License

SYNOPSIS

result = len(string)

   elemental integer function len(string)

type(unicode_type),intent(in) :: string

CHARACTERISTICS

o STRING is a scalar or array string variable
o the returned value is of default INTEGER kind

DESCRIPTION

LEN(3) returns the length of a type(unicode_type) string.

Note that unlike the intrinsic of the same name STRING needs to be defined; as the length of each element is not defined until allocated; and the KIND parameter is not available for specifying the kind of the integer returned.

OPTIONS

o STRING : A scalar or array string to return the length(s) of in glyph counts. If it is an unallocated allocatable variable or a pointer that is not associated, its length type parameter shall not be deferred.

RESULT

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

EXAMPLES

Sample program

   program demo_len
   use M_unicode, only : assignment(=), ut=>unicode_type, len
   use M_unicode, only : write(formatted)
   implicit none
   type(ut)             :: string
   type(ut),allocatable :: many_strings(:)
   integer                        :: ii
   ! BASIC USAGE
     string=’Noho me ka hau’oli’ ! (Be happy.)
     ii=len(string)
     write(*,’(DT,*(g0))’)string, ’ LEN=’, ii
   !
     string=’ How long is this allocatable string? ’
     write(*,’(DT,*(g0))’)string, ’ LEN=’, len(string)
   !
   ! STRINGS IN AN ARRAY MAY BE OF DIFFERENT LENGTHS
     many_strings = [ ut(’Tom’), ut(’Dick’), ut(’Harry’) ]
     write(*,’(*(g0,1x))’)’length of elements of array=’,len(many_strings)
   !
     write(*,’(*(g0))’)’length from type parameter inquiry=’,string%len()
   !
   ! LOOK AT HOW A PASSED STRING CAN BE USED ...
     call passed(ut(’ how long? ’))
   !
   contains
   !
   subroutine passed(str)
   type(ut),intent(in) :: str
      ! you can query the length of the passed variable
      ! when an interface is present
      write(*,’(*(g0))’)’length of passed value is ’, len(str)
   end subroutine passed
   !
   end program demo_len

Results:

   > Noho me ka hau’oli LEN=18
   >  How long is this allocatable string?  LEN=38
   > length of elements of array= 3 4 5
   > length from type parameter inquiry=38
   > length of passed value is 11

SEE ALSO

functions that perform operations on character strings:
o elemental: adjustl(3), adjustr(3), index(3), scan(3), verify(3)
o non-elemental: len_trim(3), len(3), repeat(3), trim(3)

AUTHOR

John S. Urban

LICENSE

    MIT