字符串文字中的转义序列(Fortran) [英] Escape sequence in a string literal (Fortran)

查看:147
本文介绍了字符串文字中的转义序列(Fortran)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中有一个例子

There is an example in C++

string str;
str = "First\n"
      "Second\n"
      "Third;\n";
cout << str << endl;

输出将是

First
Second
Third;  

我想在Fortran中重复它,却没有像在C ++中那样在char字符串中找到有关转义序列的任何信息.

I wanna try to repeat it in Fortran and didn't find any info about escape-sequence in char string like in C++.

推荐答案

一种方法是使用

One way would be to use achar with the ASCII code for linefeed, that is 10. The advantage of this method is, you can use other characters as necessary if you know the ASCII code.

character(len=32):: str = "First" // achar(10) // "Second"

为您提供所需的结果.(注意://是字符串联运算符)

Gives you the desired result. (Note: // is the character concatenation operator)

另一种方法是将 achar(10)替换为 new_line('a'),并且仅适用于插入换行符.

The other would be to replace achar(10) with new_line('a') and that works only for inserting linefeeds.

有趣的是,如果您使用的是 gfortran ,则可以在编译时使用选项 -fbackslash ,以使用C样式的反斜杠,如文档中所述:

Interestingly, if you're using gfortran, you can use the option -fbackslash while compiling to use C-style backslashing, as mentioned in the documentation:

-反斜杠

将字符串文字中反斜杠的解释从单个反斜杠字符更改为"C样式"转义字符.以下组合将\ a,\ b,\ f,\ n,\ r,\ t,\ v,\和\ 0扩展为ASCII字符警报,退格键,换页符,换行符,回车符,水平制表符,垂直制表符,反斜杠和NUL.此外,\ xnn,\ unnnn和\ Unnnnnnnn(其中每个n是十六进制数字)被转换为与指定代码点相对应的Unicode字符.所有其他以\开头的字符组合都不会扩展.

Change the interpretation of backslashes in string literals from a single backslash character to "C-style" escape characters. The following combinations are expanded \a, \b, \f, \n, \r, \t, \v, \, and \0 to the ASCII characters alert, backspace, form feed, newline, carriage return, horizontal tab, vertical tab, backslash, and NUL, respectively. Additionally, \xnn, \unnnn and \Unnnnnnnn (where each n is a hexadecimal digit) are translated into the Unicode characters corresponding to the specified code points. All other combinations of a character preceded by \ are unexpanded.

因此,字符串将简化为

str = "First\nSecond"

这篇关于字符串文字中的转义序列(Fortran)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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