Fortran 字符
Fortran 语言可以将字符视为单个字符或连续字符串。
字符可以是从基本字符集中提取的任何符号,即从字母、十进制数字、下划线和21个特殊字符中提取的符号。
字符常数是一个固定值的字符串。
内部数据类型字符存储字符和字符串。字符串的长度可以通过 len 说明符指定。如果没有指定长度,则为 1。您可以按位置引用字符串中的单个字符;最左侧的字符位于位置 1。
字符声明
声明字符类型数据与其他变量相同:
type-specifier :: variable_name
比如:
character :: reply, sex
可以这样赋值:
reply = 'N'sex = 'F'
下面的实例演示了字符数据类型的声明和使用:
program helloimplicit nonecharacter(len = 15) :: surname, firstnamecharacter(len = 6) :: titlecharacter(len = 25)::greetingstitle = 'Mr. 'firstname = 'Rowan 'surname = 'Atkinson'greetings = 'A big hello from Mr. Bean'print *, 'Here is ', title, firstname, surnameprint *, greetingsend program hello
结果如下:
Here is Mr. 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. Bean'print *, 'Here is ', nameprint *, greetingsend program hello
结果为:
Here is Mr.Rowan AtkinsonA big hello from Mr.Bean
一些字符函数
下表显示了一些常用的字符函数及其说明:
| 序号 | 函数 & 描述 |
|---|---|
| 1 | len(string) 它返回字符串的长度。 |
| 2 | index(string,sustring) 它在另一个字符串中查找子字符串的位置,如果找不到,则返回 0。 |
| 3 | achar(int) 它将整数转换为字符。 |
| 4 | iachar(c) 它将字符转换为整数。 |
| 5 | trim(string) 它返回去掉尾随空格的字符串。 |
| 6 | scan(string, chars) 它从左到右搜索 "string" (除非 back=.true.),以查找 "chars" 中包含的任何字符的第一次出现。它返回一个整数,给出该字符的位置,如果找不到 "chars" 中的任何字符,则返回 0。 |
| 7 | verify(string, chars) 它从左到右扫描 "string" (除非 back=.true.),以查找 "chars" 中未包含的任何字符的第一次出现。它返回一个整数,给出该字符的位置,如果只找到 "chars" 中的字符,则返回 0。 |
| 8 | adjustl(string) 它左对齐 "string" 字符串中包含的字符。 |
| 9 | adjustr(string) 它右对齐 "string" 字符串中包含的字符。 |
| 10 | len_trim(string) 它返回一个整数,等于 "string" (len(string)) 减去尾随空格的数量。 |
| 11 | repeat(string,ncopy) 它返回一个长度等于 "ncopy" 乘以 "string" 长度的字符串,并包含 "string" 的连接副本。 |
实例 1
此实例显示了 index 函数的使用:
program testingCharsimplicit nonecharacter (80) :: textinteger :: itext = 'The intrinsic data type character stores characters and strings.'i=index(text,'character')if (i /= 0) thenprint *, ' The word character found at position ',iprint *, ' in text: ', textend ifend program testingChars
结果为:
The word character found at position 25in text : The intrinsic data type character stores characters and strings.
实例 2
此实例演示了 trim 功能的使用:
program helloimplicit nonecharacter(len = 15) :: surname, firstnamecharacter(len = 6) :: titlecharacter(len = 25)::greetingstitle = 'Mr.'firstname = 'Rowan'surname = 'Atkinson'print *, 'Here is', title, firstname, surnameprint *, 'Here is', trim(title),' ',trim(firstname),' ', trim(surname)end program hello、
结果为:
Here isMr. Rowan AtkinsonHere isMr. Rowan Atkinson
实例 3
此实例演示了 achar 功能的使用:
program testingCharsimplicit nonecharacter:: chinteger:: ido i = 65, 90ch = achar(i)print*, i, ' ', chend doend program testingChars
结果为:
65 A66 B67 C68 D69 E70 F71 G72 H73 I74 J75 K76 L77 M78 N79 O80 P81 Q82 R83 S84 T85 U86 V87 W88 X89 Y90 Z
检查字符的顺序
以下函数确定字符的顺序:
| 序号 | 功能 & 描述 |
|---|---|
| 1 | lle(char, char) 比较第一个字符在词法上是否小于或等于第二个字符。 |
| 2 | lge(char, char) 比较第一个字符在词法上是否大于或等于第二个字符。 |
| 3 | lgt(char, char) 比较第一个字符在词法上是否大于第二个字符。 |
| 4 | llt(char, char) 比较第一个字符在词法上是否小于第二个字符。 |
实例 4
以下函数演示了使用方法:
program testingCharsimplicit nonecharacter:: a, b, ca = 'A'b = 'a'c = 'B'if(lgt(a,b)) thenprint *, 'A is lexically greater than a'elseprint *, 'a is lexically greater than A'end ifif(lgt(a,c)) thenprint *, 'A is lexically greater than B'elseprint *, 'B is lexically greater than A'end ifif(llt(a,b)) thenprint *, 'A is lexically less than a'end ifif(llt(a,c)) thenprint *, 'A is lexically less than B'end ifend program testingChars
结果为:
a is lexically greater than AB is lexically greater than AA is lexically less than aA is lexically less than B