REPLACE(3f) - [M_unicode:EDITING] function replaces one substring for another in string (LICENSE:MIT)
Synopsis
Characteristics
Description
Options
Returns
Examples
Author
License
syntax:
impure elemental function replace(target,old,new, & & occurrence, & & repeat, & & ignorecase, & & ierr,back) result (newline) or function replace(target,start,end,new) result newlinetype(unicode_type)|character(len=*),intent(in) :: target
type(unicode_type)|character(len=*),intent(in) :: old type(unicode_type)|character(len=*),intent(in) :: new or type(unicode_type)|character(len=*),intent(in) :: new integer, intent(in) :: start integer, intent(in) :: end
integer,intent(in),optional :: occurrence integer,intent(in),optional :: repeat logical,intent(in),optional :: ignorecase integer,intent(out),optional :: changes logical,intent(in),optional :: back character(len=:),allocatable :: newline
o TARGET,OLD and NEW may be a string or a character variable.
Replace old substring with new value in string. Either a old and new string is specified, or a new string and a column range indicating the position of the text to replace is specified.
target input line to be changed old old substring to replace new new substring start starting column of text to replace end ending column of text to replace
occurrence if present, start changing at the Nth occurrence of the OLD string. repeat number of replacements to perform. Defaults to a global replacement. ignorecase whether to ignore ASCII case or not. Defaults to .false. . back if true start replacing moving from the right end of the string moving left instead of from the left to the right.
newline allocatable string returned changes count of changes made.
Sample Program:
program demo_replace use M_unicode, only : ut=>unicode_type use M_unicode, only : unicode_type use M_unicode, only : character, replace use M_unicode, only : write(formatted) implicit none type(unicode_type) :: line ! write(*,(DT)) & & replace(ut(Xis is Xe string),ut(X),ut(th) ) write(*,(DT)) & & replace(ut(Xis is xe string),ut(x),ut(th),ignorecase=.true.) write(*,(DT)) & & replace(ut(Xis is xe string),ut(X),ut(th),ignorecase=.false.) ! ! a null old substring means "at beginning of line" write(*,(DT)) & & replace(ut(my line of text),ut(),ut(BEFORE:)) ! ! a null new string deletes occurrences of the old substring write(*,(DT)) replace(ut(I wonder i ii iii),ut(i),ut()) ! ! Examples of the use of RANGE ! line=replace(ut(aaaaaaaaa),ut(a),ut(A),occurrence=1,repeat=1) write(*,*)replace first a with A [//line%character()//] ! line=replace(ut(aaaaaaaaa),ut(a),ut(A),occurrence=3,repeat=3) write(*,*)replace a with A for 3rd to 5th occurrence [ & & //line%character()//] ! line=replace(ut(ababababa),ut(a),ut(),occurrence=3,repeat=3) write(*,*)replace a with null instances 3 to 5 [// & & line%character()//] ! line=replace( & & ut(a b ab baaa aaaa aa aa a a a aa aaaaaa),& & ut(aa),ut(CCCC),occurrence=-1,repeat=1) write(*,*)replace lastaa with CCCC [//line%character()//] ! write(*,(DT))replace(ut(myf90stuff.f90.f90),& & ut(f90),ut(for),occurrence=-1,repeat=1) write(*,(DT))replace(ut(myf90stuff.f90.f90),& & ut(f90),ut(for),occurrence=-2,repeat=2) ! end program demo_replaceResults:
> this is the string > this is the string > this is xe string > BEFORE:my line of text > I wonder > replace first a with A [Aaaaaaaaa] > replace a with A for 3rd to 5th occurrence [aaAAAaaaa] > replace a with null instances 3 to 5 [ababbb] > replace lastaa with CCCC [a b ab baaa aaaa aa aa a a a aa aaaaCCCC] > myf90stuff.f90.for > myforstuff.for.f90
John S. Urban
