-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo_adjustl.f90
executable file
·28 lines (24 loc) · 1.01 KB
/
demo_adjustl.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
program demo_adjustl
implicit none
character(len=20) :: str = ' sample string'
character(len=:),allocatable :: astr
integer :: length
! basic use
write(*,'(a,"[",a,"]")') 'original: ',str
str=adjustl(str)
write(*,'(a,"[",a,"]")') 'adjusted: ',str
! a fixed-length string can be printed
! trimmed using trim(3) or len_trim(3)
write(*,'(a,"[",a,"]")') 'trimmed: ',trim(str)
length=len_trim(str)
write(*,'(a,"[",a,"]")') 'substring:',str(:length)
! note an allocatable string stays the same length too
! and is not trimmed by just an adjustl(3) call.
astr=' allocatable string '
write(*,'(a,"[",a,"]")') 'original:',astr
astr = adjustl(astr)
write(*,'(a,"[",a,"]")') 'adjusted:',astr
! trim(3) can be used to change the length
astr = trim(astr)
write(*,'(a,"[",a,"]")') 'trimmed: ',astr
end program demo_adjustl