nospace Function

public function nospace(line)

NAME

nospace(3f) - [M_strings:WHITESPACE] remove all whitespace from
input string
(LICENSE:PD)

SYNOPSIS

function nospace(str) - remove all whitespace from input string

 character(len=*),intent(in)          :: str
 character(len=:),allocatable         :: nospace

DESCRIPTION

nospace(3f) removes space, tab, carriage return, new line, vertical
tab, formfeed and null characters (called "whitespace"). The output
is returned trimmed.

EXAMPLES

Sample program:

 program demo_nospace
 use M_strings, only: nospace
 implicit none
 character(len=:),allocatable  :: s
    s='  This     is      a     test  '
    write(*,*) 'original input string is ....',s
    write(*,*) 'processed output string is ...',nospace(s)
    if(nospace(s) == 'Thisisatest')then
       write(*,*)'nospace test passed'
    else
       write(*,*)'nospace test error'
    endif
 end program demo_nospace

Expected output

 original input string is ....  This     is      a     test
 processed output string is ...Thisisatest
 nospace test passed

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: line

Return Value character(len=:), allocatable


Contents

Source Code


Source Code

function nospace(line)

! ident_35="@(#) M_strings nospace(3f) remove all whitespace from input string"

character(len=*),intent(in)    ::  line             ! remove whitespace from this string and return it
character(len=:),allocatable   ::  nospace          ! returned string
integer                        ::  ipos             ! position to place next output character at
integer                        ::  i                ! counter to increment from beginning to end of input string
!-----------------------------------------------------------------------------------------------------------------------------------
   allocate(nospace,mold=line)                      ! initially make output line length of input line
   nospace(:len_trim(nospace))=' '
   ipos=0
   do i=1,len_trim(line)                            ! increment from first to last character of the input line
      if ( isspace( line(i:i) ) ) cycle             ! if a blank is encountered skip it
      ipos=ipos+1                                   ! increment count of non-blank characters found
      nospace(ipos:ipos)=line(i:i)                  ! store non-blank character in output
   enddo
   nospace=trim(nospace)                            ! blank out unpacked part of line
end function nospace