@@ -7,16 +7,26 @@ module strings
77 ! > @brief Count the occurrences of a substring in a string.
88 ! > @param[in] str - string to count from
99 ! > @param[in] substr - substring to count
10+ ! > @param[in] match_case - use case sensitivity (.true.) or not (.false. [DEFAULT]) \b [OPTIONAL]
1011 ! > @return count of \b substr in \b str
1112 ! -------------------------------------------------------------------------------------
12- function str_count (str ,substr ) result(count)
13+ function str_count (str ,substr , match_case ) result(count)
1314 implicit none
1415 character (len=* ), intent (in ) :: str
1516 character (len=* ), intent (in ) :: substr
17+ logical , intent (in ), optional :: match_case
18+ character (len= :), allocatable :: strtmp,substrtmp
1619 integer :: i,count
20+ if (present (match_case).and. match_case)then
21+ strtmp= str
22+ substrtmp= substr
23+ else
24+ strtmp= str_lower(str)
25+ substrtmp= str_lower(substr)
26+ endif
1727 count= 0
18- do i= 1 ,len_trim (str )
19- if (str (i:i+ (len_trim (substr )- 1 )).eq. substr )count= count+1
28+ do i= 1 ,len_trim (strtmp )
29+ if (strtmp (i:i+ (len_trim (substrtmp )- 1 )).eq. substrtmp )count= count+1
2030 end do
2131 end function str_count
2232
@@ -208,7 +218,7 @@ end function str_zfill
208218 ! > centered string is a blank character.
209219 ! > @param[in] str - string to work on
210220 ! > @param[in] width - width of centered string
211- ! > @param[in] fillchar - character to fill centered string. \b[OPTIONAL]
221+ ! > @param[in] fillchar - character to fill centered string. \b [OPTIONAL]
212222 ! > @return centered string
213223 ! -------------------------------------------------------------------------------------
214224 function str_center (str ,width ,fillchar ) result(strout)
@@ -254,18 +264,50 @@ end function str_reverse
254264 ! > @brief Return .true. if substr is in str, .false. otherwise
255265 ! > @param[in] str - string to work on
256266 ! > @param[in] substr - string to search for in str
267+ ! > @param[in] match_case - use case sensitivity (.true.) or not (.false. [DEFAULT]) \b [OPTIONAL]
257268 ! > @return .true. or .false.
258269 ! -------------------------------------------------------------------------------------
259- function str_test (str ,substr ) result(strtest)
270+ function str_test (str ,substr , match_case ) result(strtest)
260271 implicit none
261272 character (len=* ), intent (in ) :: str
262273 character (len=* ), intent (in ) :: substr
274+ logical , intent (in ), optional :: match_case
263275 logical :: strtest
264- if (str_count(str,substr).eq. 0 )then
276+ integer :: cnt
277+ cnt= 0
278+ if (present (match_case))then
279+ cnt= str_count(str,substr,match_case= match_case)
280+ else
281+ cnt= str_count(str,substr)
282+ endif
283+ if (cnt.eq. 0 )then
265284 strtest= .false.
266- elseif (str_count(str,substr) .gt. 0 )then
285+ elseif (cnt .gt. 0 )then
267286 strtest= .true.
268287 endif
269288 end function str_test
270289
290+ ! -------------------------------------------------------------------------------------
291+ ! Function: str_swapcase
292+ ! > @brief Return str with case of letters swapped
293+ ! > @param[in] str - input string
294+ ! -------------------------------------------------------------------------------------
295+ function str_swapcase (str ) result(strout)
296+ implicit none
297+ character (len=* ), intent (in ) :: str
298+ character (len= :), allocatable :: strout
299+ integer :: i,ic
300+ strout= " "
301+ do i= 1 ,len (str)
302+ ic= iachar (str(i:i))
303+ if (ic.ge. 65.and .ic.le. 90 )then
304+ strout= strout// achar (ic+32 )
305+ elseif (ic.ge. 97.and .ic.le. 122 )then
306+ strout= strout// achar (ic-32 )
307+ else
308+ strout= strout// achar (ic)
309+ endif
310+ end do
311+ end function str_swapcase
312+
271313end module strings
0 commit comments