C Library Functions  - replace (3)

NAME

replace(3f) - [M_strings:EDITING] function replaces one substring for another in string (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Returns
Examples
Author
License

SYNOPSIS

syntax:

     function replace(targetline,old,new,cmd,&
      & occurrence, &
      & repeat, &
      & ignorecase, &
      & ierr) result (newline)
     character(len=*)                       :: targetline
     character(len=*),intent(in),optional   :: old
     character(len=*),intent(in),optional   :: new
     character(len=*),intent(in),optional   :: cmd
     integer,intent(in),optional            :: occurrence
     integer,intent(in),optional            :: repeat
     logical,intent(in),optional            :: ignorecase
     integer,intent(out),optional           :: ierr
     character(len=:),allocatable           :: newline

DESCRIPTION

Replace one substring for another in string. Either CMD or OLD and NEW must be specified.

OPTIONS

targetline
  input line to be changed
old old substring to replace
new new substring
cmd alternate way to specify old and new string, in the form c/old/new/; where "/" can be any character not in "old" or "new".
occurrence
  if present, start changing at the Nth occurrence of the OLD string. If negative start replacing from the left end of the string.
repeat number of replacements to perform. Defaults to a global replacement.
ignorecase
  whether to ignore ASCII case or not. Defaults to .false. .

RETURNS

newline
  allocatable string returned
ierr error code. iF ier = -1 bad directive, >= 0 then count of changes made.

EXAMPLES

Sample Program:

   program demo_replace
   use M_strings, only : replace
   implicit none
   character(len=:),allocatable :: line

write(*,*)replace(’Xis is Xe string’,’X’,’th’) write(*,*)replace(’Xis is xe string’,’x’,’th’,ignorecase=.true.) write(*,*)replace(’Xis is xe string’,’X’,’th’,ignorecase=.false.)

! a null old substring means "at beginning of line" write(*,*) replace(’my line of text’,’’,’BEFORE:’)

! a null new string deletes occurrences of the old substring write(*,*) replace(’I wonder i ii iii’,’i’,’’)

! Examples of the use of RANGE

line=replace(’aaaaaaaaa’,’a’,’A’,occurrence=1,repeat=1) write(*,*)’replace first a with A [’//line//’]’

line=replace(’aaaaaaaaa’,’a’,’A’,occurrence=3,repeat=3) write(*,*)’replace a with A for 3rd to 5th occurrence [’//line//’]’

line=replace(’ababababa’,’a’,’’,occurrence=3,repeat=3) write(*,*)’replace a with null instances 3 to 5 [’//line//’]’

line=replace( & & ’a b ab baaa aaaa aa aa a a a aa aaaaaa’,& & ’aa’,’CCCC’,occurrence=-1,repeat=1) write(*,*)’replace lastaa with CCCC [’//line//’]’

write(*,*)replace(’myf90stuff.f90.f90’,’f90’,’for’,occurrence=-1,repeat=1) write(*,*)replace(’myf90stuff.f90.f90’,’f90’,’for’,occurrence=-2,repeat=2)

end program demo_replace

Results:

    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

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 replace (3) July 22, 2023
Generated by manServer 1.08 from df5828cc-238a-4ee6-8e47-ba02f0e02ff3 using man macros.