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世界“的子”世界“:
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
在一个字符串搜索一个子串
该指数函数有两个字符串,并检查第二个字符串是第一个字符串的子串。如果第二个参数是第一个参数的子字符串,那么它返回一个整数,这是第一个字符串中的第二个字符串的起始索引,否则返回零。
例
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
更多建议: