问题描述
我有一个非常简单的问题,我还没有找到答案。
有什么方法可以将一个字符(特别是一个空格)附加到已在Fortran中初始化的字符?
显然
CHARACTER(2000):: result
result =''
result = result //''
不起作用。
你想实现什么?当然,它有用,但它没有多大用处。尝试你在上一个问题中已经提出的方法。尤其要注意的是,所有字符串在最后一个非空格字符之后都被空格填充,这非常重要!
' '//'真的会产生'a'
但是
result = result //''
产生一个2001字符串,然后在赋值时截断,这样 result
最终是相同的。
您可能想要
result = trim(result)//''
但它也是无用的,因为字符串中填充了空格。
如果你想为了使变量更大,你必须使用:
pre $字符(:),allocatable :: result
result = ''!现在包含''并且长度为1
result = result //''!现在包含''并且长度为2
您必须启用某个过程的赋值重新分配essors。
I have a very simple problem, that I am yet to find an answer to.
Is there any way in which I can append a character (in particular, a white space) to a character that has already been initialized in Fortran?
Apparently
CHARACTER(2000) :: result
result = ''
result = result // ' '
does not work.
What do you want to achieve? Of course it works, but it has not much use. Try the approach you have been already suggested in your previous question. In particular, be aware that all strings are filled with space after their last non-space character, this is very important!
'a'//' ' really produces 'a '
but
result = result//' '
produces a 2001 character string, which is then truncated on assignment, so that result
ends up being the same.
You may want
result = trim(result)//' '
but it is also useless, because the string is filled with spaces anyway.
If you want to make the variable larger, you have to use:
character(:),allocatable:: result
result = '' !now contains ' ' and has length 1
result = result//' ' !now contains ' ' and has length 2
You have to enable reallocation on assignment on some processors.
这篇关于显然不能将字符串追加到另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!