删除字符串中的空格

问题描述 投票:0回答:6

我有以下代码:

  program main
     character (len=15) :: abc = "te st tex  t"
     print *, trim(abc)      
  end program main

哪个输出:

 te st tex  t

我排除了所有要删除的空白,但事实并非如此。如何从字符串中删除所有空格?

fortran trim fortran90 gfortran
6个回答
6
投票

修剪只会删除边缘的空格,而不是中间的空格(这是几乎所有语言/库的常见行为)。如果您想删除字符串中的所有空格,则必须创建自己的函数来执行此操作,迭代字符串。

例如:

program Test

implicit none

    ! Variables
    character(len=200) :: string

    ! Body of Test
    string = 'Hello World              7    9'
    print *, string
    call StripSpaces (string)
    print *, string


contains

    subroutine StripSpaces(string)
    character(len=*) :: string
    integer :: stringLen 
    integer :: last, actual

    stringLen = len (string)
    last = 1
    actual = 1

    do while (actual < stringLen)
        if (string(last:last) == ' ') then
            actual = actual + 1
            string(last:last) = string(actual:actual)
            string(actual:actual) = ' '
        else
            last = last + 1
            if (actual < last) &
                actual = last
        endif
    end do

    end subroutine

end program Test

这是在 intel 编译器上测试的,而不是在 gfortran 上测试的,但我认为它会起作用。


2
投票

我能够使用此处描述的变量字符串库来完成此操作(http://schonfelder.co.uk/is1539-2-99.htm)。源代码链接可以在 ISO 文档的介绍部分找到。

这是代码

程序控制台1 使用 ISO_VARYING_STRING 隐式无

! Body of Console1
character(LEN=50) :: text = 'Hello World John Mary '
character(LEN=50) :: res

  print *, trim(text)
  ! 'Hello World John Mary'
  res = REPLACE(text,' ','', every=.TRUE.)
  print *, trim(res)
  ! 'HelloWorldJohnMary'
end program Console1

1
投票

这是一种肮脏、可耻的消除空格的方法。仅当编译器以与 15 元素字符数组相同的顺序和空间布置长度为 15 的字符串时,这才有可能起作用。虽然这很可能是真的,而且根据我最近的经验也是如此,但按照标准并不能保证一定如此。除此之外,这种方法可能已经足够好了。

  ! declarations
  CHARACTER (len=15) :: abc = "te st tex  t"
  CHARACTER, DIMENSION(LEN(abc)) :: abc_array
  ! or CHARACTER, DIMENSION(:), ALLOCATABLE :: abc_array if your compiler supports
  ! automatic allocation

  ! transfer the string into an array of characters
  abc_array = TRANSFER(abc,abc_array)

  ! eliminate the spaces, and transfer back to the string
  abc = TRANSFER(PACK(abc_array,abc_array/=' '),abc)

  ! now all the spaces are at the end of abc so the following statement writes the 
  ! string with no spaces
  WRITE(*,*) TRIM(abc)

使用此方法需要您自担风险。


0
投票

对于那些不喜欢

TRANSFER
的人来说,也许一个不错的小递归函数会很有吸引力。正如所写,这取决于 Fortran 2003 自动分配字符标量的能力,但如果您的编译器尚不支持此功能,修改起来应该不会太难。

  RECURSIVE FUNCTION stripper(string,ch) RESULT(stripped)
    CHARACTER(len=*), INTENT(in) :: string
    CHARACTER, INTENT(in) :: ch
    CHARACTER(:), ALLOCATABLE :: stripped

    IF (LEN(string)==1) THEN
       IF (string==ch) THEN 
          stripped = ''
       ELSE
          stripped = string
       END IF
    ELSE
       IF (string(1:1)==ch) THEN
          stripped = stripper(string(2:),ch)
       ELSE
          stripped = string(1:1)//stripper(string(2:),ch)
       END IF
    END IF
  END FUNCTION stripper

0
投票

你可以试试这个:

program test
!erase blank space in a string
!run over every character of the string and just take every non-blank in other variable.

implicit none

character (len=100) str1,str2
integer i

str2=''                          !in this variable will be save non-blank spaces  
str1='   a   b   c  de   '            !Test string with blank spaces

write(*,*)len_trim(str1), str1

do i=1,len(str1)   
   if (str1(i:i).ne.' ')str2=trim(str2)//trim(str1(i:i))   
end do

write(*,*)len_trim(str2), str2

end 

0
投票

这里有一些 30 多年前的 f77 代码可以实现这一目的。从研究生院起就一直在使用它。它的起源已经消失在时间的迷雾中。可能是 Michael Mclennan 写的,但真的不知道。

c --------------------------------------------------------------------
c   NOSPACE (string)
c
c     This subroutine removes spaces from a character string.
c
c   INPUTS:
c        string...initial character string
c
c   OUTPUTS:
c        string...final character string with all spaces removed
c --------------------------------------------------------------------`
c
      subroutine nospace(string)
c
c     external strlen
      integer strlen
c
      character*(*) string
      integer i, j
c
      i = 1
  10  if (i.le.strlen(string)) then
        if (string(i:i).eq.' ') then
          do 20 j=i,len(string)-1
            string(j:j) = string(j+1:j+1)
  20      continue
c
        else
          i = i + 1
        endif
        goto 10
      endif
c
      return
      end
c --------------------------------------------------------------------
c   integer STRLEN (string)
c
c     The function "len" in Fortran returns the declared length
c   of the string--not the actual length of non-blank characters.
c   This function is provided for that purpose.  It returns the
c   position of the last non-blank character.
c
c   INPUTS:
c        string...character string to determine the length of
c
c   OUTPUTS:
c              ...returns the length of "string"
c --------------------------------------------------------------------
c
      integer function strlen(string)
c
      character*(*) string
c
      integer i
c
      i = len(string)
  10  if ((i.ge.1).and.(string(i:i).eq.' ')) then
        i = i - 1
        goto 10
      endif
c
      strlen = i
c
      return
      end
c

© www.soinside.com 2019 - 2024. All rights reserved.