回 帖 发 新 帖 刷新版面

主题:查找字符串数组查找问题

举例而言,就是存在一个字符串数组character*4 font(50)
存在一个搜索值check,想要查找字符串check在font中的位置,有没有内置字符串处理函数?用循环判定也可以做,但是感觉那样比较鸡肋。
谢谢。

回复列表 (共3个回复)

沙发

就用循环判断吧,别无他法。

板凳

Fortran 标准定义了如下子程序,满足阁下要求。

subroutine CharacterComputationProcedure() 
  implicit none

  character(len = 30):: str 
  character(len = 4):: cha
  integer:: Pos
  ! adjustL, adjustR


  ! INDEX
  ! Elemental Intrinsic Function (Generic): 
  ! Returns the starting position of a substring 
  ! within a string.

  ! Syntax
  ! result = INDEX (string, substring [,back] [, kind])

  ! string
  ! (Input) Must be of type character. 
 
  ! substring
  ! (Input) Must be of type character. 
 
  ! back
  ! (Input; optional) Must be of type logical. 
 
  ! kind
  ! (Input; optional) Must be a scalar integer 
  ! initialization expression.
 

  ! Results
  ! The result is of type integer. 
  ! If kind is present, the kind parameter of the result 
  ! is that specified by kind; otherwise, the kind 
  ! parameter of the result is that of default integer. 
  ! If the processor cannot represent the result value 
  ! in the kind of the result, the result is undefined. 

  ! If back does not appear (or appears with the value false), 
  ! the value returned is the minimum value of I such that 
  ! string(I : I + LEN (substring) - 1) = 
  ! substring(or zero if there is no such value). 
  ! If LEN (string) < LEN (substring), zero is returned. 
  ! If LEN (substring) = zero, 1 is returned.

  ! If back appears with the value true, 
  ! the value returned is the maximum value of 
  ! I such that string(I : I + LEN (substring) - 1) = 
  ! substring (or zero if there is no such value).
  ! If LEN(string) < LEN (substring), zero is returned. 
  ! If LEN (substring) = zero, LEN (string) + 1 is returned.
  str = "WrittenBy--ZengZhuoQuan"
  cha = "Zeng"
  Pos = index( str, substring = "Zeng", back = .false., kind = kind(1) )
  write(*, *) "Pos = ", Pos
  
  str = "Banana"
  cha = "an"
  Pos = index( str, substring = "an", back = .false., kind = kind(1) )
  write(*, *) "Pos = ", Pos
  
  return
end subroutine

3 楼

谢谢你的建议,仔细看了一下应该是不行的,我需要索引的是一个字符串数组,而并非一个字符串,需要返回的是目标字符串在字符串数组当中的位置。

我来回复

您尚未登录,请登录后再回复。点此登录或注册