Fortran |
您所在的位置:网站首页 › fortran字符串函数 › Fortran |
上一篇: Fortran - 数字
下一篇:Fortran - 字符串
Fortran - 字符
Fortran语言可以将字符视为单个字符或连续字符串. 字符可以是从基本字符集中取出的任何符号,即字母,十进制数字,下划线和21个特殊字符. 字符常量是固定值字符串. 内在数据类型字符存储字符和字符串.字符串的长度可以由 len 说明符指定.如果未指定长度,则为1.您可以按位置引用字符串中的单个字符;最左边的字符位于第1位. 字符声明声明字符类型数据与其他变量相同 : type-specifier :: variable_name例如, character :: reply, sex你可以指定一个类似的值, reply ='N' sex ='F'以下示例演示声明和使用字符数据类型 : 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. Bean' 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. Bean' print *, 'Here is ', name print *, greetings end program hello编译并执行上述程序时,它会产生以下结果 : Here is Mr.Rowan Atkinson A big hello from Mr.Bean一些字符函数下表显示了一些常用的字符函数描述 : Sr.否功能&说明1len(string) 它返回字符串的长度 2index(string,string) 它找到另一个字符串中子字符串的位置,如果找不到则返回0. 3achar(int) 它将整数转换为字符 4iachar(c) 它将一个字符转换为一个整数 5trim(string) 它返回删除了尾随空格的字符串. 6scan(string, chars) 它从左到右搜索"字符串"(除非back = .true.)对于第一次出现的任何字符包含在"字符"中编辑.它返回一个给出该字符位置的整数,如果没有找到"chars"中的字符,则返回零. 7验证(string,chars) 它会从左到右扫描"字符串"(除非返回= .true.),以便第一次出现"chars"中未包含的任何字符.它返回一个给出该字符位置的整数,如果只找到"chars"中的字符,则返回零 8adjustl(string) 它左对齐字符包含在"字符串" 9adjustr(string) 右对齐"string"中包含的字符 10len_trim(string) 它返回一个等于"string"长度的整数(len(字符串))减去尾随空白的数量 11repeat(string,ncopy) 它返回一个字符串,其长度等于"ncopy"乘以"string"的长度,并包含"ncopy"连接的"string"副本 示例1此示例显示索引的使用功能 : program testingChars implicit none character (80) :: text integer :: i text = 'The intrinsic data type character stores characters and strings.' i=index(text,'character') if (i /= 0) then print *, ' The word character found at position ',i print *, ' in text: ', text end if end program testingChars编译并执行上述程序时,它产生以下结果 : The word character found at position 25 in text : The intrinsic data type character stores characters and strings.示例2此示例演示如何使用 trim 函数 : program hello implicit none character(len = 15) :: surname, firstname character(len = 6) :: title character(len = 25)::greetings title = 'Mr.' firstname = 'Rowan' surname = 'Atkinson' print *, 'Here is', title, firstname, surname print *, 'Here is', trim(title),' ',trim(firstname),' ', trim(surname) end program hello编译时执行上面的程序,它产生以下结果 : Here isMr. Rowan Atkinson Here isMr. Rowan Atkinson示例3此示例演示了如何使用 achar 函数 : program testingChars implicit none character:: ch integer:: i do i = 65, 90 ch = achar(i) print*, i, ' ', ch end do end program testingChars编译并执行上述程序时,会产生以下结果 : 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z检查字符的词法顺序以下函数确定字符的词汇顺序 : Sr.No功能&说明1lle(char,char) 比较第一个字符是否在词法上小于或等于第二个 2lge(char,char) 比较第一个字符是否在词法上大于或等于第二个 3lgt(char,char) 比较是否第一个字符在词法上大于第二个字符 4llt(char,char) 比较第一个字符是否在词汇上小于第二个 示例4 以下函数演示了如何使用 : program testingChars implicit none character:: a, b, c a = 'A' b = 'a' c = 'B' if(lgt(a,b)) then print *, 'A is lexically greater than a' else print *, 'a is lexically greater than A' end if if(lgt(a,c)) then print *, 'A is lexically greater than B' else print *, 'B is lexically greater than A' end if if(llt(a,b)) then print *, 'A is lexically less than a' end if if(llt(a,c)) then print *, 'A is lexically less than B' end if end program testingChars当你编译并执行上面的程序产生以下结果 : a is lexically greater than A B is lexically greater than A A is lexically less than a A is lexically less than B本文地址:https://www.itbaoku.cn/tutorial/fortran-fortran_characters.html 上一篇: Fortran - 数字 下一篇:Fortran - 字符串 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |