Return a string that is a join of an array of characters.
The join-separator is set equals to a null string ‘’ if custom separator isn’t specified. The trim function is applied to array items if optional logical is_trim variable isn’t set to .false.
character(5) :: characters(3)
logical :: test_passed(13)
characters(1) = 'one'
characters(2) = 'two'
characters(3) = 'three'
test_passed(1) = (strjoin(array=characters)//''==trim(characters(1))//trim(characters(2))//trim(characters(3)))
test_passed(2) = (strjoin(array=characters, sep='-')//''==trim(characters(1))//'-'//trim(characters(2))//'-'//trim(characters(3)))
test_passed(3) = ( strjoin(array=characters, is_trim=.false.)//''==characters(1)//characters(2)//characters(3))
test_passed(4) = ( strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(1)//'-'//characters(2)//'-'//characters(3))
characters(1) = ''
characters(2) = 'two'
characters(3) = 'three'
test_passed(5) = (strjoin(array=characters)//''==trim(characters(2))//trim(characters(3)))
characters(1) = 'one'
characters(2) = 'two'
characters(3) = ''
test_passed(6) = (strjoin(array=characters)//''==trim(characters(1))//trim(characters(2)))
characters(1) = 'one'
characters(2) = ''
characters(3) = 'three'
test_passed(7) = (strjoin(array=characters)//''==trim(characters(1))//trim(characters(3)))
characters(1) = ''
characters(2) = 'two'
characters(3) = 'three'
test_passed(8) = (strjoin(array=characters, sep='-')//''==trim(characters(2))//'-'//trim(characters(3)))
characters(1) = 'one'
characters(2) = 'two'
characters(3) = ''
test_passed(9) = (strjoin(array=characters, sep='-')//''==trim(characters(1))//'-'//trim(characters(2)))
characters(1) = 'one'
characters(2) = ''
characters(3) = 'three'
test_passed(10) = (strjoin(array=characters, sep='-')//''==trim(characters(1))//'-'//trim(characters(3)))
characters(1) = ''
characters(2) = 'two'
characters(3) = 'three'
test_passed(11) = (strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(2)//'-'//characters(3))
characters(1) = 'one'
characters(2) = 'two'
characters(3) = ''
test_passed(12) = (strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(1)//'-'//characters(2))
characters(1) = 'one'
characters(2) = ''
characters(3) = 'three'
test_passed(13) = (strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(1)//'-'//characters(3))
print '(L1)', all(test_passed)
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(kind=CK, len=*), | intent(in) | :: | array(1:) |
Array to be joined. |
||
character(kind=CK, len=*), | intent(in), | optional | :: | sep |
Separator. |
|
logical, | intent(in), | optional | :: | is_trim |
Flag to setup trim character or not |
The join of array.
pure function strjoin_characters(array, sep, is_trim) result(join) !< Return a string that is a join of an array of characters. !< !< The join-separator is set equals to a null string '' if custom separator isn't specified. !< The trim function is applied to array items if optional logical is_trim variable isn't set to .false. !< !<```fortran !< character(5) :: characters(3) !< logical :: test_passed(13) !< characters(1) = 'one' !< characters(2) = 'two' !< characters(3) = 'three' !< test_passed(1) = (strjoin(array=characters)//''==trim(characters(1))//trim(characters(2))//trim(characters(3))) !< test_passed(2) = (strjoin(array=characters, sep='-')//''==trim(characters(1))//'-'//trim(characters(2))//'-'//trim(characters(3))) !< test_passed(3) = ( strjoin(array=characters, is_trim=.false.)//''==characters(1)//characters(2)//characters(3)) !< test_passed(4) = ( strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(1)//'-'//characters(2)//'-'//characters(3)) !< characters(1) = '' !< characters(2) = 'two' !< characters(3) = 'three' !< test_passed(5) = (strjoin(array=characters)//''==trim(characters(2))//trim(characters(3))) !< characters(1) = 'one' !< characters(2) = 'two' !< characters(3) = '' !< test_passed(6) = (strjoin(array=characters)//''==trim(characters(1))//trim(characters(2))) !< characters(1) = 'one' !< characters(2) = '' !< characters(3) = 'three' !< test_passed(7) = (strjoin(array=characters)//''==trim(characters(1))//trim(characters(3))) !< characters(1) = '' !< characters(2) = 'two' !< characters(3) = 'three' !< test_passed(8) = (strjoin(array=characters, sep='-')//''==trim(characters(2))//'-'//trim(characters(3))) !< characters(1) = 'one' !< characters(2) = 'two' !< characters(3) = '' !< test_passed(9) = (strjoin(array=characters, sep='-')//''==trim(characters(1))//'-'//trim(characters(2))) !< characters(1) = 'one' !< characters(2) = '' !< characters(3) = 'three' !< test_passed(10) = (strjoin(array=characters, sep='-')//''==trim(characters(1))//'-'//trim(characters(3))) !< characters(1) = '' !< characters(2) = 'two' !< characters(3) = 'three' !< test_passed(11) = (strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(2)//'-'//characters(3)) !< characters(1) = 'one' !< characters(2) = 'two' !< characters(3) = '' !< test_passed(12) = (strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(1)//'-'//characters(2)) !< characters(1) = 'one' !< characters(2) = '' !< characters(3) = 'three' !< test_passed(13) = (strjoin(array=characters, sep='-', is_trim=.false.)//''==characters(1)//'-'//characters(3)) !< print '(L1)', all(test_passed) !<``` !=> T <<< character(kind=CK, len=*), intent(in) :: array(1:) !< Array to be joined. character(kind=CK, len=*), intent(in), optional :: sep !< Separator. logical, intent(in), optional :: is_trim !< Flag to setup trim character or not type(string) :: join !< The join of array. character(kind=CK, len=:), allocatable :: sep_ !< Separator, default value. logical :: is_trim_ !< Flag to setup trim character or not integer :: a !< Counter. sep_ = '' if (present(sep)) sep_ = sep is_trim_ = .true. ; if (present(is_trim)) is_trim_ = is_trim join = '' if (is_trim_) then do a=2, size(array, dim=1) if (trim(array(a))/='') join%raw = join%raw//sep_//trim(array(a)) enddo if (trim(array(1))/='') then join%raw = trim(array(1))//join%raw else join%raw = join%raw(len(sep_)+1:len(join%raw)) endif else do a=2, size(array, dim=1) if (array(a)/='') join%raw = join%raw//sep_//array(a) enddo if (array(1)/='') then join%raw = array(1)//join%raw else join%raw = join%raw(len(sep_)+1:len(join%raw)) endif endif endfunction strjoin_characters