Fortran語(yǔ)言可以把字符作為單個(gè)字符或字符串連片。
的字符串可以是只有一個(gè)長(zhǎng)度符,或者它甚至可以是零長(zhǎng)度。在Fortran中,字符常量是一對(duì)雙引號(hào)或單引號(hào)之間給出。
內(nèi)部數(shù)據(jù)類型存儲(chǔ)字符的字符和字符串。字符串的長(zhǎng)度可以通過LEN說明指定。如果未指定長(zhǎng)度,它是1。您可以一個(gè)字符串按位置指內(nèi)引用單個(gè)字符;最左邊的字符的位置是1。
聲明一個(gè)字符串是一樣的其他變量:
type-specifier :: variable_name
例如,
Character(len=20) :: firstname, surname
您可以分配一個(gè)值一樣,
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
連接運(yùn)算符//,連接字符串。
下面的例子說明了這一點(diǎn):
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中,可以通過索引字符串,給人的開始和子的一對(duì)括號(hào)的結(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ù),得到的日期和時(shí)間的字符串。我們使用范圍說明符單獨(dú)提取年份,日期,月份,小時(shí),分鐘和第二個(gè)信息。
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ì)的日期和時(shí)間信息:
Date String: 20140803 Year: 2014 Month: 08 Day: 03 Time String: 075835.466 Hour: 07 Minute: 58 Second: 35.466
TRIM函數(shù)接受一個(gè)字符串,并刪除所有尾隨空格后返回輸入字符串。
例
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!
該功能adjustl將一個(gè)字符串通過刪除前導(dǎo)空格和追加他們?yōu)槲搽S空白返回。
該功能adjustr將一個(gè)字符串通過去除尾隨空白和追加他們?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ù)有兩個(gè)字符串,并檢查第二個(gè)字符串是第一個(gè)字符串的子串。如果第二個(gè)參數(shù)是第一個(gè)參數(shù)的子字符串,那么它返回一個(gè)整數(shù),這是第一個(gè)字符串中的第二個(gè)字符串的起始索引,否則返回零。
例
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
更多建議: