Skip to content

Commit 44fbce2

Browse files
committed
Added str_test function
Added new function, str_test, to test the if a substring is in a string (.true.), otherwise (.false.). strings.f90 - Added str_test -- basically a wrapper for str_count and returning a .true./.false. based on returned count of substring. Updated docs and tests.
1 parent 1862f61 commit 44fbce2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ The function names are prefixed with ``str_``. The following is a list of avail
1919
* **str_zfill** - Pad a string with zeroes ("0") to specified width. If width is <= input string width, then the original string is returned.
2020
* **str_center** - Center a string to a specified width. The default character to fill in the centered string is a blank character.
2121
* **str_reverse** - Reverse a string.
22+
* **str_test** - Return .true. is a substring is found in a string, .false. otherwise.
2223

23-
All functions return a deferred-length, allocatable character scalar (``character(len=:), allocatable``) with the exception of **``str_count``** which returns an integer.
24+
All functions return a deferred-length, allocatable character scalar (``character(len=:), allocatable``) with the exception of **``str_count``** which returns an integer and **``str_test``** which returns a logical.
2425

2526
## Requirements
2627

strings.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,23 @@ function str_reverse(str) result(strout)
249249
end do
250250
end function str_reverse
251251

252+
! -------------------------------------------------------------------------------------
253+
! Function: str_test
254+
!> @brief Return .true. if substr is in str, .false. otherwise
255+
!> @param[in] str - string to work on
256+
!> @param[in] substr - string to search for in str
257+
!> @return .true. or .false.
258+
! -------------------------------------------------------------------------------------
259+
function str_test(str,substr) result(strtest)
260+
implicit none
261+
character(len=*), intent(in) :: str
262+
character(len=*), intent(in) :: substr
263+
logical :: strtest
264+
if(str_count(str,substr).eq.0)then
265+
strtest=.false.
266+
elseif(str_count(str,substr).gt.0)then
267+
strtest=.true.
268+
endif
269+
end function str_test
270+
252271
end module strings

test/test.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ program test
44

55
character(len=:), allocatable :: c1,c2
66
integer :: i1
7+
logical :: l1
78

89
c1="My name is Tony Stark. Tony Stark is Iron Man."
910
!print *,"MYSTRING = ",c1
@@ -60,4 +61,12 @@ program test
6061
c2=str_reverse(c1)
6162
!print *,"STR_REVERSE = ",":",c2,":"
6263

64+
c1="Ironman,Thor,Thanos,Black Panther,Winter Soldier"
65+
l1=str_test(c1,"Thor") ! TRUE
66+
!print *,l1
67+
if(.not.l1)call exit(1)
68+
l1=str_test(c1,"Vision") ! FALSE
69+
!print *,l1
70+
if(l1)call exit(1)
71+
6372
end program test

0 commit comments

Comments
 (0)