strip Function

private elemental function strip(self, remove_nulls)

Return a copy of the string with the leading and trailing characters removed.

 type(string) :: astring
 logical      :: test_passed(1)
 astring = '  Hello World!   '
 test_passed(1) = astring%strip()//''=='Hello World!'
 print '(L1)', all(test_passed)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self

The string.

logical, intent(in), optional :: remove_nulls

Remove null characters at the end.

Return Value type(string)

The stripped string.


Called by

proc~~strip~~CalledByGraph proc~strip stringifor_string_t::string%strip proc~decode stringifor_string_t::string%decode proc~decode->proc~strip

Contents

Source Code


Source Code

   elemental function strip(self, remove_nulls)
   !< Return a copy of the string with the leading and trailing characters removed.
   !<
   !< @note Multiple subsequent separators are collapsed to one occurence.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(1)
   !< astring = '  Hello World!   '
   !< test_passed(1) = astring%strip()//''=='Hello World!'
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string), intent(in)           :: self         !< The string.
   logical,       intent(in), optional :: remove_nulls !< Remove null characters at the end.
   type(string)                        :: strip        !< The stripped string.
   integer                             :: c            !< Counter.

   if (allocated(self%raw)) then
      strip = self%adjustl()
      strip = strip%trim()
      if (present(remove_nulls)) then
         if (remove_nulls) then
            c = index(self%raw, char(0))
            if (c>0) strip%raw = strip%raw(1:c-1)
         endif
      endif
   endif
   endfunction strip