如何在Fortran中合并两个字符串 [英] How to combine two strings in Fortran

查看:1304
本文介绍了如何在Fortran中合并两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要在Fortran中执行的操作的python3示例:

Here's an example in python3 of what I want to do in Fortran:

str1 = "Hello"
str2 = " World!"
print(str1 + str2)

# And then the result would be "Hello World!"

当我这样做时:

print "(A)", str1, str2

将其放在单独的行上.如果有人知道如何帮助,请回答.

It puts it on a separate line. If anyone knows how to help please answer.

推荐答案

在另一个答案中给出了使用//运算符对字符串连接的字面答案.请特别注意,您可能想TRIM第一个参数.

The literal answer to string concatenation, using the // operator, is given in another answer. Note, particularly, that you likely want to TRIM the first argument.

但是您的问题还有另一个有趣的概念,那就是格式还原.

But there is another interesting concept your question raises, and that is format reversion.

使用格式(A),我们有一个格式项.在输出列表str1, str2中,我们有两个输出项.在一般的输出语句中,我们将每个格式项(带有重复计数)应用于相应的输出项.因此,使用第一个格式项A处理str1,并出现一个字符串.

With the format (A) we have one format item. In the output list str1, str2 we have two output items. In a general output statement we apply each format item (with repeat counts) to a corresponding output item. So, str1 is processed with the first format item A, and a string appears.

进入第二个输出项str2,我们已经使用了单个格式项,到达格式项列表的末尾.结果是我们看到了这种格式转换:也就是说,我们返回到列表中的第一项.之后,至关重要的是,我们开始新的一行.

Come the second output item str2 we've already used the single format item, reaching the end of the format item list. The result is that we see this format reversion: that is, we go back to the first item in the list. After, crucially, we start a new line.

因此,如果我们只想将这两个项目打印到一行(它们之间没有空格或空白行),则可以使用(为清晰起见,忽略修剪)

So, if we just want to print those two items to one line (with no space or blank line between them) we could use (neglecting trimming for clarity)

print "(A)", str1//str2

或者我们可以使用没有此版本的格式

or we could use a format which hasn't this reversion

print "(2A)", str1, str2
print "(A, A)", str1, str2

第一个将两个字符变量连接在一起,给出一个更长的字符变量,然后将其打印为单个输出项.第二个分别打印.

The first concatenates the two character variables to give one, longer, which is then printed as a single output item. The second prints both individually.

介绍您的特定示例

character(12), parameter :: str1="Hello"    ! Intentionally longer - trailing blanks
character(12), parameter :: str2=" World!"

print "(2A)", TRIM(str1), TRIM(str2)
end

将具有类似

Hello World!

带有中间空格,因为TRIM不会从str2中删除前导空格.更广泛地说,尽管我们没有领先的空间,但我们希望将其添加到输出中.

with that middle space because TRIM won't remove the leading space from str2. More widely, though we won't have the leading space there for us, and we want to add it in the output.

自然地,串联仍然有效(我回到假定不修剪的状态)

Naturally, concatenation still works (I'm back to assuming no-trimming)

character(*), parameter :: str1="Hello"    ! No trailing blank
character(*), parameter :: str2="World!"

print "(A)", str1//" "//str2
end

,但是我们可以使用X编辑描述符选择格式,以添加空格

but we can choose our format, using the X edit descriptor, to add a space

print "(2(A,1X))", str1, str2
print "(A,1X,A)", str1, str2
print "(2(A,:,1X))", str1, str2

其中最后一个具有有用的冒号编辑描述符(此答案的范围之外).

where this final one has the useful colon edit descriptor (outside scope of this answer).

这篇关于如何在Fortran中合并两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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