I find Python’s string handling capabilities very good. I missed those useful string methods in Fortran, so I decided to implement them. At the moment I have these functions:
public :: & !# Python equivalents:
capitalize, & !# "anna".capitalize() -> "Anna"
center, & !# "*".center(3) -> " * "
count_elems, & !# "Anna".count("n")
endswith, & !# "01.png".endswith(".png") -> True
equal_strings, & !# same length .and. same content
explode, & !# list("abc") -> ["a", "b", "c"]
find, & !# "Anna".find("n") -> 1 (Python is 0-based)
isascii, & !# "Éva".isascii() -> False
isdigit, & !# "2026".isdigit() -> True
is_in, & !# "prog" in "programming" -> True
islower, & !# "anna".islower() -> True
isspace, & !# " \t \r\n" -> True
isupper, & !# "ANNA".isupper() -> True
lower, & !# "aNNa".lower() -> "anna"
lstrip, & !# " \t anna " -> "anna "
removeprefix, & !# "01.jpg".removeprefix("01") -> ".jpg"
removesuffix, & !# "01.jpg".removesuffix(".jpg") -> "01"
replace, & !# "cat dog cat".replace("cat", "kitten") -> "kitten dog kitten"
rev, & !# "abcd"[::-1] -> "dcba"
rfind, & !# "Anna".rfind("n") -> 2 (Python is 0-based)
rstrip, & !# " anna \n" -> " anna"
slice, & !# like in Python: s[1:5:2], or s[10:2:-2]
split, & !# " aa bb cc ".split() -> ["aa", "bb", "cc"]
startswith, & !# "01.png".endswith("01") -> True
strip, & !# " \t aa \t \n".strip() -> "aa"
swapcase, & !# "Anna".swapcase() -> "aNNA"
upper, & !# "Anna".upper() -> "ANNA"
zfill !# "7".zfill(3) -> "007
The source code is here: jstring.f90. Test cases are here: test_jstring.F90.
I would like to get some feedback. What else should be added? How could I improve it?