显然无法将字符串附加到另一个 [英] Apparently not able to append a string to another

查看:30
本文介绍了显然无法将字符串附加到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题,我还没有找到答案.

I have a very simple problem, that I am yet to find an answer to.

有什么方法可以将字符(特别是空格)附加到已经在 Fortran 中初始化的字符上?

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?

显然

CHARACTER(2000) :: result
result = ''
result = result // ' '

不工作.

推荐答案

你想实现什么?当然可以,但是用处不大.尝试您在上一个问题中已经建议的方法.特别要注意的是,所有字符串在最后一个非空格字符后都用空格填充,这一点非常重要!

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 '

但是

result = result//' '

产生一个 2001 的字符串,然后在赋值时截断,因此 result 最终是相同的.

produces a 2001 character string, which is then truncated on assignment, so that result ends up being the same.

你可能想要

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.

这篇关于显然无法将字符串附加到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆