Fortran - 字串



Fortran 語言可以將字元視為單個字元或連續字串。

字元字串的長度可以只有一個字元,甚至可以為零長度。在 Fortran 中,字元常量用一對雙引號或單引號括起來。

內在資料型別character儲存字元和字串。字串的長度可以透過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 isMr.   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)

   !  let’s 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

   !  let’s 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
廣告