h =&gt; TH    : '//trim(targetline)

 ! a null old substring means "at beginning of line"
 ! changes the input to 'BEFORE:this is the input string'
 call substitute(targetline,'','BEFORE:')
 write(*,*)'"" =&gt; BEFORE: '//trim(targetline)

 ! a null new string deletes occurrences of the old substring
 ! changes the input to 'ths s the nput strng'
 call substitute(targetline,'i','')
 write(*,*)'i =&gt; ""     : '//trim(targetline)

 end program demo_substitute

Expected output

 ORIGINAL    : this is the inp