Fortran

您所在的位置:网站首页 fortran字符串函数 Fortran

Fortran

2023-08-23 03:17| 来源: 网络整理| 查看: 265

上一篇: 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.否功能&说明1

len(string)

它返回字符串的长度

2

index(string,string)

它找到另一个字符串中子字符串的位置,如果找不到则返回0.

3

achar(int)

它将整数转换为字符

4

iachar(c)

它将一个字符转换为一个整数

5

trim(string)

它返回删除了尾随空格的字符串.

6

scan(string, chars)

它从左到右搜索"字符串"(除非back = .true.)对于第一次出现的任何字符包含在"字符"中编辑.它返回一个给出该字符位置的整数,如果没有找到"chars"中的字符,则返回零.

7

验证(string,chars)

它会从左到右扫描"字符串"(除非返回= .true.),以便第一次出现"chars"中未包含的任何字符.它返回一个给出该字符位置的整数,如果只找到"chars"中的字符,则返回零

8

adjustl(string)

它左对齐字符包含在"字符串"

9

adjustr(string)

右对齐"string"中包含的字符

10

len_trim(string)

它返回一个等于"string"长度的整数(len(字符串))减去尾随空白的数量

11

repeat(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功能&说明1

lle(char,char)

比较第一个字符是否在词法上小于或等于第二个

2

lge(char,char)

比较第一个字符是否在词法上大于或等于第二个

3

lgt(char,char)

比较是否第一个字符在词法上大于第二个字符

4

llt(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