Fortran分割读取字符串数字混合文本行

利用函数index:

INDEX(STRING,SUBSTRING,BACK)

purpose: return the starting position of a substring within a string

               When BACK missing or BACK=.False. , return the first starting postion of substring, if no substring in string or LEN(string)<LEN(substring), return 0, if LEN(substring)=0, return 1 ;

     When BACK=.True., return the last starting position of substring. The return value is similar as .False. case.

 

EXAMPLE: 文本文件

                xyz   x   1   y   2   z   3

    分割提取字符和数字

 1 program main
 2     implicit none
 3     character(len=100) :: str
 4     integer :: x,y,z,id
 5     character(len=3) :: xyz
 6     
 7      read(*,'(a)')  str
 8      id=index(str,'xyz',BACK=.False.)
 9      xyz=str(id:id+2)
10      str(id:id+2)=''
11      id=index(str,'x')
12      x=str(id)
13      str(id)=' '
14      id=index(str,'y')
15      y=str(id)
16      str(id)=' '
17      id=index(str,'z')
18      z=str(id)
19      str(id)=' '
20      read(str,*) x,y,z
21 
22 end program main

 

 

             

 

posted @ 2016-11-03 18:49  H.*n  阅读(3929)  评论(0编辑  收藏  举报