As @Vladimir says in his answer, string case-sensitvity does matter in Fortran for string comparison. Moreover, trailing blanks are ignored in string comparison in Fortran, that is, "SKPS"=="SKPS "
evaluates to .true.
(the same does NOT hold for leading blanks). Here is an efficient implementation of what you need:
module String_mod
implicit none
public
contains
pure function getLowerCase(string) result(output)
! convert string to lower-case
character(*), intent(in) :: string
integer, parameter :: DUC = ichar('A') - ichar('a')
character(len(string)) :: output
character :: ch
integer :: i
do i = 1,len(string)
ch = string(i:i)
if (ch>='A' .and. ch<='Z') ch = char(ichar(ch)-DUC)
output(i:i) = ch
end do
end function getLowerCase
pure function getUpperCase(string) result(output)
! convert string to upper-case
character(*), intent(in) :: string
integer, parameter :: DUC = ichar('A') - ichar('a')
character(len(string)) :: output
character :: ch
integer :: i
do i = 1,len(string)
ch = string(i:i)
if (ch>='a' .and. ch<='z') ch = char(ichar(ch)+DUC)
output(i:i) = ch
end do
end function getUpperCase
end module String_mod
program stringComparison
use, intrinsic :: iso_fortran_env, only: output_unit
use String_mod, only: getLowerCase
implicit none
character(:), allocatable :: thisString, thatString
thisString = "STACKOVERFLOW"
thatString = "StackOverflow"
if (getLowerCase(thisString)==getLowerCase(thatString)) then
write(output_unit,"(*(g0,:,' '))") "Ignoring case-sensitivity, the strings are the same:" &
, '"'//thisString//'"', "==", '"'//thatString//'"'
else
write(output_unit,"(*(g0,:,' '))") "strings are NOT the same:", '"'//thisString//'"', "/=", '"'//thatString//'"'
end if
! Be mindful of trailing white-space characters. They do not matter in string comparison.
thisString = "STACKOVERFLOW"
thatString = "StackOverflow "
if (getLowerCase(thisString)==getLowerCase(thatString)) then
write(output_unit,"(*(g0,:,' '))") "Ignoring case-sensitivity, the strings are the same:" &
, '"'//thisString//'"', "==", '"'//thatString//'"'
else
write(output_unit,"(*(g0,:,' '))") "strings are NOT the same:", '"'//thisString//'"', "/=", '"'//thatString//'"'
end if
! Be mindful of leading white-space characters. They are important in string comparison.
thisString = "STACKOVERFLOW"
thatString = " StackOverflow"
if (getLowerCase(thisString)==getLowerCase(thatString)) then
write(output_unit,"(*(g0,:,' '))") "Ignoring case-sensitivity, the strings are the same:" &
, '"'//thisString//'"', "==", '"'//thatString//'"'
else
write(output_unit,"(*(g0,:,' '))") "strings are NOT the same:", '"'//thisString//'"', "/=", '"'//thatString//'"'
end if
end program stringComparison
Compiling and running the above code gives:
Ignoring case-sensitivity, the strings are the same: "STACKOVERFLOW" == "StackOverflow"
Ignoring case-sensitivity, the strings are the same: "STACKOVERFLOW" == "StackOverflow "
strings are NOT the same: "STACKOVERFLOW" /= " StackOverflow"
If this is a performance-critical section of the code, you could likely achieve even better performance to by inlining the getLowerCase()
function and modifying it to directly compare the ASCII character codes of the letters. But is it really worth it? It depends on the specifics of your problem.
You can test the above code here: https://www.tutorialspoint.com/compile_fortran_online.php