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) ! 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