@@ -10,6 +10,7 @@ module stdlib_strings
1010 private
1111
1212 public :: strip, chomp
13+ public :: starts_with, ends_with
1314
1415
1516 ! > Remove leading and trailing whitespace characters.
@@ -36,6 +37,28 @@ module stdlib_strings
3637 end interface chomp
3738
3839
40+ ! > Check whether a string starts with substring or not
41+ ! >
42+ ! > Version: experimental
43+ interface starts_with
44+ module procedure :: starts_with_string_string
45+ module procedure :: starts_with_string_char
46+ module procedure :: starts_with_char_string
47+ module procedure :: starts_with_char_char
48+ end interface starts_with
49+
50+
51+ ! > Check whether a string ends with substring or not
52+ ! >
53+ ! > Version: experimental
54+ interface ends_with
55+ module procedure :: ends_with_string_string
56+ module procedure :: ends_with_string_char
57+ module procedure :: ends_with_char_string
58+ module procedure :: ends_with_char_char
59+ end interface ends_with
60+
61+
3962contains
4063
4164
@@ -173,4 +196,99 @@ pure function set_to_string(set) result(string)
173196 end function set_to_string
174197
175198
199+ ! > Check whether a string starts with substring or not
200+ pure function starts_with_char_char (string , substring ) result(match)
201+ character (len=* ), intent (in ) :: string
202+ character (len=* ), intent (in ) :: substring
203+ logical :: match
204+ integer :: nsub
205+
206+ nsub = len (substring)
207+ if (len (string) < nsub) then
208+ match = .false.
209+ return
210+ end if
211+ match = string (1 :nsub) == substring
212+
213+ end function starts_with_char_char
214+
215+ ! > Check whether a string starts with substring or not
216+ elemental function starts_with_string_char (string , substring ) result(match)
217+ type (string_type), intent (in ) :: string
218+ character (len=* ), intent (in ) :: substring
219+ logical :: match
220+
221+ match = starts_with(char (string), substring)
222+
223+ end function starts_with_string_char
224+
225+ ! > Check whether a string starts with substring or not
226+ elemental function starts_with_char_string (string , substring ) result(match)
227+ character (len=* ), intent (in ) :: string
228+ type (string_type), intent (in ) :: substring
229+ logical :: match
230+
231+ match = starts_with(string, char (substring))
232+
233+ end function starts_with_char_string
234+
235+ ! > Check whether a string starts with substring or not
236+ elemental function starts_with_string_string (string , substring ) result(match)
237+ type (string_type), intent (in ) :: string
238+ type (string_type), intent (in ) :: substring
239+ logical :: match
240+
241+ match = starts_with(char (string), char (substring))
242+
243+ end function starts_with_string_string
244+
245+
246+ ! > Check whether a string ends with substring or not
247+ pure function ends_with_char_char (string , substring ) result(match)
248+ character (len=* ), intent (in ) :: string
249+ character (len=* ), intent (in ) :: substring
250+ logical :: match
251+ integer :: last, nsub
252+
253+ last = len (string)
254+ nsub = len (substring)
255+ if (last < nsub) then
256+ match = .false.
257+ return
258+ end if
259+ match = string (last- nsub+1 :last) == substring
260+
261+ end function ends_with_char_char
262+
263+ ! > Check whether a string ends with substring or not
264+ elemental function ends_with_string_char (string , substring ) result(match)
265+ type (string_type), intent (in ) :: string
266+ character (len=* ), intent (in ) :: substring
267+ logical :: match
268+
269+ match = ends_with(char (string), substring)
270+
271+ end function ends_with_string_char
272+
273+ ! > Check whether a string ends with substring or not
274+ elemental function ends_with_char_string (string , substring ) result(match)
275+ character (len=* ), intent (in ) :: string
276+ type (string_type), intent (in ) :: substring
277+ logical :: match
278+
279+ match = ends_with(string, char (substring))
280+
281+ end function ends_with_char_string
282+
283+ ! > Check whether a string ends with substring or not
284+ elemental function ends_with_string_string (string , substring ) result(match)
285+ type (string_type), intent (in ) :: string
286+ type (string_type), intent (in ) :: substring
287+ logical :: match
288+
289+ match = ends_with(char (string), char (substring))
290+
291+ end function ends_with_string_string
292+
293+
176294end module stdlib_strings
0 commit comments