Fortran 字符串
Fortran 语言可以将字符视为单个字符或连续字符串。
字符串的长度可能只有一个字符,甚至可能为 0。在 Fortran 中,字符常数在双引号或单引号之间给出。
内部数据类型字符存储字符和字符串。字符串的长度可以通过 len 说明符指定。如果没有指定长度,则为 1。您可以按位置引用字符串中的单个字符;最左侧的字符位于位置 1。
字符串声明
声明字符串与其他变量相同:
type-specifier :: variable_name
例如:
Character(len = 20) :: firstname, surname
也可以这样赋值:
character (len = 40) :: namename = "Zara Ali"
以下实例演示了字符数据类型的声明和使用:
program helloimplicit nonecharacter(len = 15) :: surname, firstnamecharacter(len = 6) :: titlecharacter(len = 25)::greetingstitle = 'Mr.'firstname = 'Rowan'surname = 'Atkinson'greetings = 'A big hello from Mr. Beans'print *, 'Here is', title, firstname, surnameprint *, greetingsend program hello
结果为:
Here isMr. Rowan AtkinsonA big hello from Mr. Bean
连接字符串
连接运算符 // 用于连接字符串。
以下实例演示了这一点:
program helloimplicit nonecharacter(len = 15) :: surname, firstnamecharacter(len = 6) :: titlecharacter(len = 40):: namecharacter(len = 25)::greetingstitle = 'Mr.'firstname = 'Rowan'surname = 'Atkinson'name = title//firstname//surnamegreetings = 'A big hello from Mr. Beans'print *, 'Here is', nameprint *, greetingsend program hello
结果为:
Here is Mr. Rowan AtkinsonA big hello from Mr. Bean
提取子字符串
在 Fortran 中,您可以通过对字符串进行索引来从字符串中提取子字符串,在一对括号中给出子字符串的开始和结束索引。这被称为范围说明符。
以下实例显示了如何从字符串 "hello world" 中提取子字符串 "world":
program subStringcharacter(len = 11)::hellohello = "Hello World"print*, hello(7:11)end program subString
结果为:
World
实例
以下实例使用 date_and_time 函数给出日期和时间字符串。我们使用范围说明符分别提取年、日期、月、小时、分钟和秒信息。
program datetimeimplicit nonecharacter(len = 8) :: dateinfo ! ccyymmddcharacter(len = 4) :: year, month*2, day*2character(len = 10) :: timeinfo ! hhmmss.ssscharacter(len = 2) :: hour, minute, second*6call 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 = dayyear = dateinfo(1:4)month = dateinfo(5:6)day = dateinfo(7:8)print*, 'Date String:', dateinfoprint*, 'Year:', yearprint *,'Month:', monthprint *,'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 = secondhour = timeinfo(1:2)minute = timeinfo(3:4)second = timeinfo(5:10)print*, 'Time String:', timeinfoprint*, 'Hour:', hourprint*, 'Minute:', minuteprint*, 'Second:', secondend program datetime
结果为:
Date String: 20140803Year: 2014Month: 08Day: 03Time String: 075835.466Hour: 07Minute: 58Second: 35.466
裁剪字符串
trim 函数接受一个字符串,并在删除所有尾随空格后返回输入字符串。
实例
program trimStringimplicit nonecharacter (len = *), parameter :: fname="Susanne", sname="Rizwan"character (len = 20) :: fullnamefullname = fname//" "//sname !concatenating the stringsprint*,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 接受一个字符串,并通过删除前导空格并将其作为尾随空格附加来返回它。
函数 adjuster 接受一个字符串,并通过删除尾随空格并将其作为前导空格附加来返回它。
program helloimplicit nonecharacter(len = 15) :: surname, firstnamecharacter(len = 6) :: titlecharacter(len = 40):: namecharacter(len = 25):: greetingstitle = 'Mr. 'firstname = 'Rowan'surname = 'Atkinson'greetings = 'A big hello from Mr. Beans'name = adjustl(title)//adjustl(firstname)//adjustl(surname)print *, 'Here is', nameprint *, greetingsname = adjustr(title)//adjustr(firstname)//adjustr(surname)print *, 'Here is', nameprint *, greetingsname = trim(title)//trim(firstname)//trim(surname)print *, 'Here is', nameprint *, greetingsend program hello
结果为:
Here is Mr. Rowan AtkinsonA big hello from Mr. BeanHere is Mr. Rowan AtkinsonA big hello from Mr. BeanHere is Mr.RowanAtkinsonA big hello from Mr. Bean
在字符串中搜索子字符串
index 函数接收两个字符串,并检查第二个字符串是否是第一个字符串的子字符串。如果第二个参数是第一个参数的子字符串,则返回一个整数,该整数是第一个字符中第二个字符的起始索引,否则返回 0。
实例
program helloimplicit nonecharacter(len=30) :: myStringcharacter(len=10) :: testStringmyString = 'This is a test'testString = 'test'if(index(myString, testString) == 0)thenprint *, 'test is not found'elseprint *, 'test is found at index: ', index(myString, testString)end ifend program hello
结果为:
test is found at index: 11