Fortran語言的字符串

2018-12-12 14:24 更新

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

的字符串可以是只有一個長度符,或者它甚至可以是零長度。在Fortran中,字符常量是一對雙引號或單引號之間給出。

內(nèi)部數(shù)據(jù)類型存儲字符的字符和字符串。字符串的長度可以通過LEN說明指定。如果未指定長度,它是1。您可以一個字符串按位置指內(nèi)引用單個字符;最左邊的字符的位置是1。

串宣言

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

type-specifier :: variable_name

例如,

Character(len=20) :: firstname, surname

您可以分配一個值一樣,

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

下面的例子演示了聲明和使用字符數(shù)據(jù)類型:

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

當(dāng)你編譯和執(zhí)行上面的程序它產(chǎn)生以下結(jié)果:

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

當(dāng)你編譯和執(zhí)行上面的程序它產(chǎn)生以下結(jié)果:

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

提取子字符串

在Fortran中,可以通過索引字符串,給人的開始和子的一對括號的結(jié)束索引從字符串中提取子串。這就是所謂的程度上說明。

下面的例子演示了如何提取字符串'hello世界“的子”世界“:

program subString

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

當(dāng)你編譯和執(zhí)行上面的程序它產(chǎn)生以下結(jié)果:

World

下面的示例使用DATE_AND_TIME函數(shù),得到的日期和時間的字符串。我們使用范圍說明符單獨提取年份,日期,月份,小時,分鐘和第二個信息。

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

當(dāng)你編譯和執(zhí)行上面的程序,它提供了詳細(xì)的日期和時間信息:

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

修剪字符串

TRIM函數(shù)接受一個字符串,并刪除所有尾隨空格后返回輸入字符串。

program trimString
implicit none

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

當(dāng)你編譯和執(zhí)行上面的程序它產(chǎn)生以下結(jié)果:

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

左和字符串的調(diào)整權(quán)

該功能adjustl將一個字符串通過刪除前導(dǎo)空格和追加他們?yōu)槲搽S空白返回。

該功能adjustr將一個字符串通過去除尾隨空白和追加他們?yōu)榍皩?dǎo)空格返回。

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

當(dāng)你編譯和執(zhí)行上面的程序它產(chǎn)生以下結(jié)果:

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

在一個字符串搜索一個子串

該指數(shù)函數(shù)有兩個字符串,并檢查第二個字符串是第一個字符串的子串。如果第二個參數(shù)是第一個參數(shù)的子字符串,那么它返回一個整數(shù),這是第一個字符串中的第二個字符串的起始索引,否則返回零。

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

當(dāng)你編譯和執(zhí)行上面的程序它產(chǎn)生以下結(jié)果:

test is found at index: 11

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號