A real string type
One type holding a string of any length: arrays whose elements have different lengths, no trim() everywhere, no silent truncation.
A first string
A string type for modern Fortran with the methods you miss from Python: split, join, replace, strip, slice, justify, cast to and from numbers, read and write files. One type, one module, pure Fortran 2008.
A real session with a small StringiFor program: every answer is one method of the string type.
This is the whole program: a select case on the command, one method for each answer.
program sf
!< Quick start: a tiny command line tool, one StringiFor method for each command.
use stringifor
implicit none
type(string) :: command, text, other
type(string), allocatable :: lines(:)
integer :: l
command = argument(1)
text = argument(2)
select case(command%chars())
case('upper')
call show(text%upper()//'')
case('snake')
call show(text%snakecase()//'')
case('reverse')
call show(text%reverse_words()//'')
case('justify')
call text%justify(lines=lines, width=34)
do l = 1, size(lines)
call show('|'//lines(l)//'|')
enddo
case('newer')
other = argument(3)
call show(merge(text, other, text%compare_version(other) > 0)//'')
case('hex')
call show('0x'//text%hex())
case('number')
if (text%is_integer()) then
call show('an integer')
elseif (text%is_real()) then
call show('a real')
else
call show('not a number')
endif
endselect
contains
function argument(n) result(arg)
!< The n-th command line argument, as a string.
integer, intent(in) :: n
type(string) :: arg
character(:), allocatable :: buffer
integer :: length
call get_command_argument(n, length=length)
allocate(character(length) :: buffer)
call get_command_argument(n, value=buffer)
arg = buffer
endfunction argument
subroutine show(answer)
!< Print an answer, in colour.
character(*), intent(in) :: answer
type(string) :: coloured
coloured = answer
print '(A)', coloured%colorize(color_fg='cyan', style='bold_on')
endsubroutine show
endprogram sfThe same methods take you from a line of text to a report read from a file, cleaned, computed and laid out. This is the output of the program that the tutorial builds step by step:
Learn StringiFor step by step in the tutorial, find quick answers in the cookbook, look up every detail in the reference.
Contributions are welcome โ see the Contributing page.
This project is distributed under a multi-licensing system:
Anyone interested in using, developing, or contributing to this project is welcome โ pick the license that best fits your needs.