Fortran字符返回函数未知长度 [英] Fortran character-returning function of unknown length

查看:262
本文介绍了Fortran字符返回函数未知长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用未知长度的字符函数?
在trim()函数的例子中,我明白,可以不指定返回字符串的长度。

How to use character function of unknown length? On example of trim() function, I understand, that it is possible not to specify the length of returning string.

例如:

write (*,*) trim(str)

将只返回字符串的一部分,而不包含空格。

Will return only part of the string without closing spaces.

此函数不知道返回字符串的长度调用。

This function does not have any idea about the length of returning string before the call.

trim()函数有限制?

更多的变体是找到trim()函数的原始代码。

On more variant is to find original code of trim() function.

我找到了(在fortran中返回未知长度的字符串),但这不是我的问题的答案。

I have found (Returning character string of unknown length in fortran) but it is not the answer to my question.

当然,我想写函数,它通过整数返回字符。

To be sure, I want to write function, that returns string by integer number.

这样的东西:

function strByInt(myInt)
...
write (strByInt,fmt) myInt; return
end function strByInt

somewhere else:

write (*,*) strByInt(50) ! will write '50'


推荐答案

它?它提到具有延迟长度的可分配字符。见下面我实现我定期使用:

That question you referenced partially answers it? It mentions the allocatable characters with deferred length. See below my implementation I use regularly:

  function strByInt(i) result(res)
    character(:),allocatable :: res
    integer,intent(in) :: i
    character(range(i)+2) :: tmp
    write(tmp,'(i0)') i
    res = trim(tmp)
  end function

结果变量在赋值时分配

trim 函数是一个不同的野兽,作为一个内在函数不必在Fortran中编程,并且可以遵守不同的规则。它只是返回它需要返回。但它可以像上面很容易实现。

The trim function is a different beast, as an intrinsic function it doesn't have to be programmed in Fortran and can obey different rules. It just returns what it needs to return. But it could be as well implemented as above quite easily.

这篇关于Fortran字符返回函数未知长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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