Removing whitespace in string

。_饼干妹妹 提交于 2019-12-09 19:25:43

问题


I have the following code:

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

Which outputs:

 te st tex  t

I excepted all the whitespace to be removed but it wasn't. How can I remove all the whitespace from the string?


回答1:


Trim will remove spaces only at the edges, not in the middle (this is common behaviour on almost all languages/libraries). If you want to remove all spaces in the string, you will have to create your own function to do this, iterating through the string.

Ex.:

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

This was tested on intel compiler, not on gfortran, but I think it will work.




回答2:


Here's a dirty, shameful way to eliminate the spaces. This is only likely to work if a compiler lays out a length-15 string in the same order and space as it would a 15-element array of characters. While this is likely to be true, and in my recent experience is true, it is not guaranteed to be so by the standard. That aside, this approach may be good enough.

  ! 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)

Use this approach at your own risk.




回答3:


I was able to do this using the variable string library described here ( http://schonfelder.co.uk/is1539-2-99.htm ). The source code link is found in the introduction section of the ISO document.

Here is the code

program Console1 use ISO_VARYING_STRING implicit none

! 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



回答4:


For those averse to TRANSFER perhaps a nice little recursive function would appeal. As written this depends on Fortran 2003's ability to automatically allocate character scalars, but it shouldn't be too hard to modify if your compiler doesn't support this feature yet.

  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



回答5:


You can try this:

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 


来源:https://stackoverflow.com/questions/27179549/removing-whitespace-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!