replace Function

public pure function replace(str, search, repla) result(res)

Searches and replaces a substring in a string. It replaces all occurences.

Syntax

 t = replace(str, search, repla)

Where:

  • str: String to modify.
  • search: String to search for in str.
  • repla: String to replace in str..

It returns the modified modified string.

Example

The following program searches and replaces in a string:

 PROGRAM replaceExample
    USE FU_Strings, ONLY: replace
    IMPLICIT NONE
    CHARACTER(LEN=:), ALLOCATABLE :: text
    CHARACTER(LEN=:), ALLOCATABLE :: modified_text
    text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, &
       &sed do eiusmod tempor incididunt ut labore et dolore magna al&
       &iqua. Ut enim ad minim veniam, quis nostrud exercitation ulla&
       &mco laboris nisi ut aliquip ex ea commodo consequat. Duis aut&
       &e irure dolor in reprehenderit in voluptate velit esse cillum&
       & dolore eu fugiat nulla pariatur. Excepteur sint occaecat cup&
       &idatat non proident, sunt in culpa qui officia deserunt molli&
       &t anim id est laborum."
    modified_text = replace(text, 'm', 'X')
    WRITE(*,*) modified_text
    modified_text = replace(text, ' ad ', ' AD ')
    WRITE(*,*) modified_text
 END PROGRAM replaceExample

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str

String to modify

character(len=*), intent(in) :: search

String to search for in str.

character(len=*), intent(in) :: repla

String to replace in str.

Return Value character(len=:), ALLOCATABLE

Modified string.


Contents