位置:首頁 > 高級語言 > Fortran教學 > Fortran字符串

Fortran字符串

Fortran語言可以把字符作為單個字符或連續的字符串。

字符串可以是隻有一個長度的字符,或者它甚至可以是零長度。在Fortran語言,字符常量是一對雙引號或單引號之間字符內容。

內部數據類型的字符存儲字符和字符串。字符串的長度可以通過len個符來指定。如果冇有指定長度,它是長度是1. 可以將字符串按位置指的是指在單個字符;最左邊的字符的位置是1。

字符串聲明

聲明一個字符串跟其他變量是一樣的:

type-specifier :: variable_name

例如,

Character(len=20) :: firstname, surname

可以指定一個值類似,

character (len=40) :: name  
name = Zara Ali

下麵的例子演示了聲明和使用字符數據類型:

program hello
implicit none

   character(len=15) :: surname, firstname 
   character(len=6) :: title 
   character(len=25)::greetings
   
   title = 'Mr.' 
   firstname = 'Rowan' 
   surname = 'Atkinson'
   greetings = 'A big hello from Mr. Beans'
   
   print *, 'Here is', title, firstname, surname
   print *, greetings
   
end program hello

當編譯並執行上述程序,將產生以下結果:

Here is Mr. Rowan Atkinson       
A big hello from Mr. Bean

字符串連接

連接運算符//,連接字符串。

下麵的例子說明了這一點:

program hello
implicit none

   character(len=15) :: surname, firstname 
   character(len=6) :: title 
   character(len=40):: name
   character(len=25)::greetings
   
   title = 'Mr.' 
   firstname = 'Rowan' 
   surname = 'Atkinson'
   
   name = title//firstname//surname
   greetings = 'A big hello from Mr. Beans'
   
   print *, 'Here is', name
   print *, greetings
   
end program hello

當編譯並執行上述程序,將產生以下結果:

Here is Mr. Rowan Atkinson       
A big hello from Mr. Bean

提取子串

在Fortran中,可以通過索引的字符串,開始和子串一對括號的結束索引,從字符串中提取一個子字符串。這就是所謂的範圍說明。

下麵的示例顯示了如何提取字符串'Hello World'的子字符串“world”:

program subString

   character(len=11)::hello
   hello = "Hello World"
   print*, hello(7:11)
   
end program subString 

當編譯並執行上述程序,將產生以下結果:

World

例子

下麵的示例使用 date_and_time 函數,得到日期和時間的字符串。我們使用範圍說明符單獨提取年份,日期,月份,小時,分鐘和秒的信息。

program  datetime
implicit none

   character(len = 8) :: dateinfo ! ccyymmdd
   character(len = 4) :: year, month*2, day*2

   character(len = 10) :: timeinfo ! hhmmss.sss
   character(len = 2)  :: hour, minute, second*6

   call  date_and_time(dateinfo, timeinfo)

   !  lets break dateinfo into year, month and day.
   !  dateinfo has a form of ccyymmdd, where cc = century, yy = year
   !  mm = month and dd = day

   year  = dateinfo(1:4)
   month = dateinfo(5:6)
   day   = dateinfo(7:8)

   print*, 'Date String:', dateinfo
   print*, 'Year:', year
   print *,'Month:', month
   print *,'Day:', day

   !  lets break timeinfo into hour, minute and second.
   !  timeinfo has a form of hhmmss.sss, where h = hour, m = minute
   !  and s = second

   hour   = timeinfo(1:2)
   minute = timeinfo(3:4)
   second = timeinfo(5:10)

   print*, 'Time String:', timeinfo
   print*, 'Hour:', hour
   print*, 'Minute:', minute
   print*, 'Second:', second   
   
end program  datetime

當編譯並執行上述程序,它提供了詳細的日期和時間信息:

Date String: 20140803
   Year: 2014
   Month: 08
   Day: 03
   Time String: 075835.466
   Hour: 07
   Minute: 58
   Second: 35.466

字符串修整

trim函數接受一個字符串,並刪除所有尾隨空格後返回輸入字符串。

例子

program trimString
implicit none

   character (len=*), parameter :: fname="Susanne", sname="Rizwan"
   character (len=20) :: fullname 
   
   fullname=fname//" "//sname !concatenating the strings
   
   print*,fullname,", the beautiful dancer from the east!"
   print*,trim(fullname),", the beautiful dancer from the east!"
   
end program trimString

當編譯並執行上述程序,將產生以下結果:

Susanne Rizwan, the beautiful dancer from the east!
Susanne Rizwan, the beautiful dancer from the east!

字符串左右調整

函數 adjustl 需要一個字符串,並通過去除前導空格,並追加其作為尾隨空白返回。

函數 adjustr 需要一個字符串,並通過刪除尾隨空格和追加作為前導空格返回。

例子

program hello
implicit none

   character(len=15) :: surname, firstname 
   character(len=6) :: title 
   character(len=40):: name
   character(len=25):: greetings
   
   title = 'Mr. ' 
   firstname = 'Rowan' 
   surname = 'Atkinson'
   greetings = 'A big hello from Mr. Beans'
   
   name = adjustl(title)//adjustl(firstname)//adjustl(surname)
   print *, 'Here is', name
   print *, greetings
   
   name = adjustr(title)//adjustr(firstname)//adjustr(surname)
   print *, 'Here is', name
   print *, greetings
   
   name = trim(title)//trim(firstname)//trim(surname)
   print *, 'Here is', name
   print *, greetings
   
end program hello

當編譯並執行上述程序,將產生以下結果:

Here is Mr. Rowan  Atkinson           
A big hello from Mr. Bean
Here is Mr. Rowan Atkinson    
A big hello from Mr. Bean
Here is Mr.RowanAtkinson                        
A big hello from Mr. Bean

搜索字符串的子串

index 函數有兩個字符串,並檢查是否第二個字符串的第一個字符串的子串。如果第二個參數是第一個參數的子字符串,然後返回一個整數,是第一個字符串第二個字符串的開始索引,否則返回零。

例子

program hello
implicit none

   character(len=30) :: myString
   character(len=10) :: testString
   
   myString = 'This is a test'
   testString = 'test'
   
   if(index(myString, testString) == 0)then
      print *, 'test is not found'
   else
      print *, 'test is found at index: ', index(myString, testString)
   end if
   
end program hello

當編譯並執行上述程序,將產生以下結果:

test is found at index: 11