lchop Function

public pure function lchop(str, chars) result(newstr)

Chop leading chars string from str. Note that trailing spaces are not ignored in either string.

Arguments

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

original string

character(len=*), intent(in) :: chars

characters to strip

Return Value character(len=:), allocatable

new string


Called by

proc~~lchop~~CalledByGraph proc~lchop lchop proc~rchop rchop proc~rchop->proc~lchop

Source Code

    pure function lchop(str, chars) result(newstr)

    character(len=*),intent(in) :: str  !! original string
    character(len=*),intent(in) :: chars !! characters to strip
    character(len=:),allocatable :: newstr !! new string

    ! this logic here is to account for trailing spaces, which we preserve
    if (len(chars)>len(str)) then
        newstr = str  ! not possible to chop
    else
        if (str==chars) then
            if (len(str)>len(chars)) then
                newstr = str(len(chars)+1:)  ! only trailing spaces remain
            else
                newstr = ''  ! string is now empty
            end if
        else
            if (index(str,chars) == 1) then
                newstr = str(len(chars)+1:)  ! chop leading chars, keep rest of string
            else
                newstr = str  ! original string, noting to chop
            end if
        end if
    end if

    end function lchop