Fortran 字符串

Fortran 语言可以将字符视为单个字符或连续字符串。

字符串的长度可能只有一个字符,甚至可能为 0。在 Fortran 中,字符常数在双引号或单引号之间给出。

内部数据类型字符存储字符和字符串。字符串的长度可以通过 len 说明符指定。如果没有指定长度,则为 1。您可以按位置引用字符串中的单个字符;最左侧的字符位于位置 1。

字符串声明

声明字符串与其他变量相同:

  1. type-specifier :: variable_name

例如:

  1. Character(len = 20) :: firstname, surname

也可以这样赋值:

  1. character (len = 40) :: name
  2. name = "Zara Ali"

以下实例演示了字符数据类型的声明和使用:

  1. program hello
  2. implicit none
  3. character(len = 15) :: surname, firstname
  4. character(len = 6) :: title
  5. character(len = 25)::greetings
  6. title = 'Mr.'
  7. firstname = 'Rowan'
  8. surname = 'Atkinson'
  9. greetings = 'A big hello from Mr. Beans'
  10. print *, 'Here is', title, firstname, surname
  11. print *, greetings
  12. end program hello

结果为:

  1. Here isMr. Rowan Atkinson
  2. A big hello from Mr. Bean

连接字符串

连接运算符 // 用于连接字符串。

以下实例演示了这一点:

  1. program hello
  2. implicit none
  3. character(len = 15) :: surname, firstname
  4. character(len = 6) :: title
  5. character(len = 40):: name
  6. character(len = 25)::greetings
  7. title = 'Mr.'
  8. firstname = 'Rowan'
  9. surname = 'Atkinson'
  10. name = title//firstname//surname
  11. greetings = 'A big hello from Mr. Beans'
  12. print *, 'Here is', name
  13. print *, greetings
  14. end program hello

结果为:

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

提取子字符串

在 Fortran 中,您可以通过对字符串进行索引来从字符串中提取子字符串,在一对括号中给出子字符串的开始和结束索引。这被称为范围说明符。

以下实例显示了如何从字符串 "hello world" 中提取子字符串 "world":

  1. program subString
  2. character(len = 11)::hello
  3. hello = "Hello World"
  4. print*, hello(7:11)
  5. end program subString

结果为:

  1. World

实例

以下实例使用 date_and_time 函数给出日期和时间字符串。我们使用范围说明符分别提取年、日期、月、小时、分钟和秒信息。

  1. program datetime
  2. implicit none
  3. character(len = 8) :: dateinfo ! ccyymmdd
  4. character(len = 4) :: year, month*2, day*2
  5. character(len = 10) :: timeinfo ! hhmmss.sss
  6. character(len = 2) :: hour, minute, second*6
  7. call date_and_time(dateinfo, timeinfo)
  8. ! lets break dateinfo into year, month and day.
  9. ! dateinfo has a form of ccyymmdd, where cc = century, yy = year
  10. ! mm = month and dd = day
  11. year = dateinfo(1:4)
  12. month = dateinfo(5:6)
  13. day = dateinfo(7:8)
  14. print*, 'Date String:', dateinfo
  15. print*, 'Year:', year
  16. print *,'Month:', month
  17. print *,'Day:', day
  18. ! lets break timeinfo into hour, minute and second.
  19. ! timeinfo has a form of hhmmss.sss, where h = hour, m = minute
  20. ! and s = second
  21. hour = timeinfo(1:2)
  22. minute = timeinfo(3:4)
  23. second = timeinfo(5:10)
  24. print*, 'Time String:', timeinfo
  25. print*, 'Hour:', hour
  26. print*, 'Minute:', minute
  27. print*, 'Second:', second
  28. end program datetime

结果为:

  1. Date String: 20140803
  2. Year: 2014
  3. Month: 08
  4. Day: 03
  5. Time String: 075835.466
  6. Hour: 07
  7. Minute: 58
  8. Second: 35.466

裁剪字符串

trim 函数接受一个字符串,并在删除所有尾随空格后返回输入字符串。

实例

  1. program trimString
  2. implicit none
  3. character (len = *), parameter :: fname="Susanne", sname="Rizwan"
  4. character (len = 20) :: fullname
  5. fullname = fname//" "//sname !concatenating the strings
  6. print*,fullname,", the beautiful dancer from the east!"
  7. print*,trim(fullname),", the beautiful dancer from the east!"
  8. end program trimString

结果为:

  1. Susanne Rizwan , the beautiful dancer from the east!
  2. Susanne Rizwan, the beautiful dancer from the east!

字符串左右调整函数 adjustl 接受一个字符串,并通过删除前导空格并将其作为尾随空格附加来返回它。

函数 adjuster 接受一个字符串,并通过删除尾随空格并将其作为前导空格附加来返回它。

  1. program hello
  2. implicit none
  3. character(len = 15) :: surname, firstname
  4. character(len = 6) :: title
  5. character(len = 40):: name
  6. character(len = 25):: greetings
  7. title = 'Mr. '
  8. firstname = 'Rowan'
  9. surname = 'Atkinson'
  10. greetings = 'A big hello from Mr. Beans'
  11. name = adjustl(title)//adjustl(firstname)//adjustl(surname)
  12. print *, 'Here is', name
  13. print *, greetings
  14. name = adjustr(title)//adjustr(firstname)//adjustr(surname)
  15. print *, 'Here is', name
  16. print *, greetings
  17. name = trim(title)//trim(firstname)//trim(surname)
  18. print *, 'Here is', name
  19. print *, greetings
  20. end program hello

结果为:

  1. Here is Mr. Rowan Atkinson
  2. A big hello from Mr. Bean
  3. Here is Mr. Rowan Atkinson
  4. A big hello from Mr. Bean
  5. Here is Mr.RowanAtkinson
  6. A big hello from Mr. Bean

在字符串中搜索子字符串

index 函数接收两个字符串,并检查第二个字符串是否是第一个字符串的子字符串。如果第二个参数是第一个参数的子字符串,则返回一个整数,该整数是第一个字符中第二个字符的起始索引,否则返回 0。

实例
  1. program hello
  2. implicit none
  3. character(len=30) :: myString
  4. character(len=10) :: testString
  5. myString = 'This is a test'
  6. testString = 'test'
  7. if(index(myString, testString) == 0)then
  8. print *, 'test is not found'
  9. else
  10. print *, 'test is found at index: ', index(myString, testString)
  11. end if
  12. end program hello

结果为:

  1. test is found at index: 11

分类导航