substitute(3f) - [M_strings:EDITING] subroutine globally substitutes one substring for another in string (LICENSE:PD)
Synopsis
Description
Options
Examples
Author
License
impure elemental subroutine substitute(targetline,old,new,ierr,start,end)
character(len=*) :: targetline character(len=*),intent(in) :: old character(len=*),intent(in) :: new integer,intent(out),optional :: ierr integer,intent(in),optional :: start integer,intent(in),optional :: end
Globally substitute one substring for another in string.
TARGETLINE input line to be changed. Must be long enough to hold altered output. OLD substring to find and replace NEW replacement for OLD substring IERR error code. If IER = -1 bad directive, >= 0 then count of changes made. START sets the left margin to be scanned for OLD in TARGETLINE. END sets the right margin to be scanned for OLD in TARGETLINE.
Sample Program:
program demo_substitute use M_strings, only : substitute implicit none ! must be long enough to hold changed line character(len=80) :: targetlineExpected outputtargetline=this is the input string write(*,*)ORIGINAL : //trim(targetline)
! changes the input to THis is THe input string call substitute(targetline,th,TH) write(*,*)th => 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(*,*)"" => 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 => "" : //trim(targetline)
end program demo_substitute
ORIGINAL : this is the input string th => TH : THis is THe input string "" => BEFORE: BEFORE:THis is THe input string i => "" : BEFORE:THs s THe nput strng
John S. Urban
Public Domain
Nemo Release 3.1 | substitute (3m_strings) | January 10, 2025 |