Skip to content

implemented low-level replace_all function #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 4, 2021
Prev Previous commit
Next Next commit
documented replace_all function
  • Loading branch information
aman-godara committed Jun 22, 2021
commit e2a75a4abb162edbf7c06cc814ce503173b6f0b7
58 changes: 56 additions & 2 deletions doc/specs/stdlib_strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ Default value of `occurrence` is set to `1`.
If `consider_overlapping` is not provided or is set to `.true.` the function counts two overlapping occurrences of substring as two different occurrences.
If `occurrence`th occurrence is not found, function returns `0`.


#### Syntax

`string = [[stdlib_strings(module):find(interface)]] (string, pattern [, occurrence, consider_overlapping])`
Expand Down Expand Up @@ -318,7 +317,7 @@ program demo_find
use stdlib_string_type, only: string_type, assignment(=)
use stdlib_strings, only : find
implicit none
string_type :: string
type(string_type) :: string

string = "needle in the character-stack"

Expand All @@ -328,3 +327,58 @@ program demo_find

end program demo_find
```


<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
### `replace_all`

#### Description

Replaces all occurrences of substring `pattern` in the input `string` with the replacement `replacement`.
Occurrences overlapping on a base occurrence will not be replaced.

#### Syntax

`string = [[stdlib_strings(module):replace_all(interface)]] (string, pattern, replacement)`

#### Status

Experimental

#### Class

Pure function

#### Argument

- `string`: Character scalar or [[stdlib_string_type(module):string_type(type)]].
This argument is intent(in).
- `pattern`: Character scalar or [[stdlib_string_type(module):string_type(type)]].
This argument is intent(in).
- `replacement`: Character scalar or [[stdlib_string_type(module):string_type(type)]].
This argument is intent(in).

#### Result value

The result is of the same type as `string`.

#### Example

```fortran
program demo_replace_all
use stdlib_string_type, only: string_type, assignment(=)
use stdlib_strings, only : replace_all
implicit none
type(string_type) :: string

string = "hurdles here, hurdles there, hurdles everwhere"
! string <-- "hurdles here, hurdles there, hurdles everwhere"

print '(a)', replace_all(string, "hurdles", "learn from")
! "learn from here, learn from there, learn from everwhere"

string = replace_all(string, "hurdles", "technology")
! string <-- "technology here, technology there, technology everywhere"

end program demo_replace_all
```