Fortran 删除字符串中的空白
我有以下代码:Fortran 删除字符串中的空白,fortran,trim,fortran90,gfortran,Fortran,Trim,Fortran90,Gfortran,我有以下代码: program main character (len=15) :: abc = "te st tex t" print *, trim(abc) end program main 哪些产出: te st tex t 我希望所有的空格都能被删除,但事实并非如此。如何删除字符串中的所有空白> Primm只删除边缘,而不是中间(这是几乎所有语言/库中常见的行为)。如果要删除字符串中的所有空格,则必须创建自己的函数来执行此操作,遍历字符
program main
character (len=15) :: abc = "te st tex t"
print *, trim(abc)
end program main
哪些产出:
te st tex t
我希望所有的空格都能被删除,但事实并非如此。如何删除字符串中的所有空白> Primm只删除边缘,而不是中间(这是几乎所有语言/库中常见的行为)。如果要删除字符串中的所有空格,则必须创建自己的函数来执行此操作,遍历字符串 例:
程序测试
隐式无
! 变量
字符(len=200)::字符串
! 测试主体
字符串='Hello World 7 9'
打印*,字符串
调用条带空间(字符串)
打印*,字符串
包含
子例程条带空间(字符串)
字符(len=*)::字符串
整数::stringLen
整数::最后一个,实际值
stringLen=len(字符串)
最后=1
实际值=1
执行时(实际
这是在英特尔编译器上测试的,不是在gfortran上,但我认为它可以工作。我可以使用这里描述的变量字符串库()来完成这项工作。源代码链接可在ISO文档的简介部分找到 这是密码 程序控制台1 使用ISO_可变_字符串 隐式无
! 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
这里有一个肮脏的,可耻的方式来消除空间。只有当编译器按照与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)
使用这种方法的风险由您自己承担。对于那些反对
传输的人来说
可能需要一个很好的递归函数。如前所述,这取决于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
您可以尝试以下方法:
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
可能您需要这样做:(description,)准确地说
trim
只删除尾随空格,而不是前导空格。同样,这个答案并没有解决如何删除所有空格的问题(至少没有详细说明)。这个示例对您足够详细,ja72?:)你能用WHERE
语句和字符数组也过滤掉空格吗?WHERE(abc_数组/='')abc_数组2=abc_数组END WHERE
然后abc=TRANSFER(abc_数组2,…)
。大概对于我来说,其中
是一个过滤器PACK
就像是一种重塑,而对传输的磨磨蹭蹭也让我感到肮脏和羞耻,标准对此有一些支持,因为字符标量的布局必须与默认字符数组的布局基本相同,参数关联的序列关联规则才能工作(使用这些规则是传输的另一种选择,但您需要一个中间过程调用)。
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