比较Fortran中的两个字符串 [英] Comparing two strings in Fortran

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

问题描述

根据字母顺序比较两个字符串abcbcd的正确方法是什么?有内置命令可以这样做吗?还是>.lt.可以正常工作?

What is the correct way to compare two strings say abc and bcd depending on the alphabetic order? Is there a built in command to do so? Or would > or .lt. do the work without any problems?

推荐答案

内在关系运算符.lt.<(以及等于"和大于"朋友)确实可以用于比较字符变量

The intrinsic relational operators .lt. and < (along with the "equal" and "greater than" friends) indeed may be used to compare character variables.

我们看到了定义(Fortran 2018,10.1.5.5.1):

We see the definition (Fortran 2018, 10.1.5.5.1):

如果在此位置上x1的字符值在整理序列中早于x2的值,则认为字符操作数x1小于x2

the character operand x1 is considered to be less than x2 if the character value of x1 at this position precedes the value of x2 in the collating sequence

其中比较是通过不同字符串中的第一个字符部分完成的.

where the comparison is done with the first character part in the corresponding strings which differ.

整理顺序告诉您例如'a'是否在'b'之前.因此,如果将'abc''bcd'进行比较,则比较是在'a''b'之间.

The collating sequence tells you whether, for example, 'a' precedes 'b'. So, if 'abc' is compared with 'bcd' then the comparison is between 'a' and 'b'.

如果要比较的两个字符串的长度不同,则执行比较时,就像在较短的字符串的右侧填充空格(空格)以使其与较长的字符串一样.这意味着在比较'ab''abc'时,我们将查看'ab ''abc':'ab'<'abc'当且仅当' '<'c'.

If the two strings to be compared are of different lengths, then the comparison is performed as though the shorter string is padded with blanks (spaces) on the right to make it the same length of the longer. This means that when comparing 'ab' and 'abc' we look at 'ab ' and 'abc': 'ab'<'abc' if and only if ' '<'c'.

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

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