Apparently not able to append a string to another

2020-04-08 14:25发布

问题:

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.

回答1:

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.