在Linux系統下,使用gfortran編譯器進行Fortran程序開發時,可以通過以下方法進行字符串處理:
program main
character(len=20) :: str1, str2, result_str
str1 = "Hello, "
str2 = "World!"
result_str = str1 // str2
print *, result_str
end program main
program main
use iso_fortran_env, only: int32
implicit none
character(len=20) :: str
integer(int32) :: str_length
str = "Hello, World!"
str_length = len_trim(str)
print *, "String length: ", str_length
end program main
program main
character(len=20) :: str1, str2
logical :: is_equal
str1 = "Hello, World!"
str2 = "Hello, World!"
is_equal = str1 == str2
print *, "Strings are equal: ", is_equal
end program main
program main
character(len=20) :: str, substr
str = "Hello, World!"
substr = str(1:5)
print *, "Substring: ", substr
end program main
program main
use iso_fortran_env, only: int32
implicit none
character(len=20) :: str, target
integer(int32) :: pos
str = "Hello, World!"
target = "World"
pos = index(str, target)
print *, "Position of target: ", pos
end program main
program main
character(len=20) :: str, old_substr, new_substr, result_str
integer :: pos
str = "Hello, World!"
old_substr = "World"
new_substr = "Fortran"
pos = index(str, old_substr)
if (pos > 0) then
result_str = str(1:pos-1) // new_substr // str(pos+len(old_substr):)
else
result_str = str
end if
print *, "Replaced string: ", result_str
end program main
這些示例展示了如何在Linux系統下使用gfortran編譯器進行基本的字符串處理。你可以根據需要修改和擴展這些代碼來滿足你的實際需求。