Skip to content

Commit 7af51a6

Browse files
committed
add function to replace characters in strings
1 parent cf49f39 commit 7af51a6

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

fpm/src/fpm_strings.f90

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module fpm_strings
55
private
66
public :: f_string, lower, split, str_ends_with, string_t
77
public :: string_array_contains, string_cat, len_trim, operator(.in.), fnv_1a
8-
public :: resize, str
8+
public :: replace, resize, str
99

1010
type string_t
1111
character(len=:), allocatable :: s
@@ -335,6 +335,20 @@ subroutine split(input_line,array,delimiters,order,nulls)
335335
enddo
336336
end subroutine split
337337

338+
pure function replace(string, charset, target_char) result(res)
339+
! Returns string with characters in charset replaced with target_char.
340+
character(*), intent(in) :: string
341+
character, intent(in) :: charset(:), target_char
342+
character(len(string)) :: res
343+
integer :: n
344+
res = string
345+
do n = 1, len(string)
346+
if (any(string(n:n) == charset)) then
347+
res(n:n) = target_char
348+
end if
349+
end do
350+
end function replace
351+
338352
subroutine resize_string(list, n)
339353
!> Instance of the array to be resized
340354
type(string_t), allocatable, intent(inout) :: list(:)

0 commit comments

Comments
 (0)